1 /*
  2  * Copyright (c) 2023, 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 <cds/archiveBuilder.hpp>
 26 #include <classfile/systemDictionaryShared.hpp>
 27 #include <compiler/compileBroker.hpp>
 28 #include "precompiled.hpp"
 29 #include "ci/ciEnv.hpp"
 30 #include "ci/ciMetadata.hpp"
 31 #include "cds/archiveBuilder.hpp"
 32 #include "cds/cdsConfig.hpp"
 33 #include "cds/metaspaceShared.hpp"
 34 #include "cds/methodDataDictionary.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/recompilationSchedule.hpp"
 49 #include "oops/trainingData.hpp"
 50 #include "runtime/arguments.hpp"
 51 #include "runtime/fieldDescriptor.inline.hpp"
 52 #include "runtime/javaThread.inline.hpp"
 53 #include "runtime/jniHandles.inline.hpp"
 54 #include "runtime/os.hpp"
 55 #include "utilities/growableArray.hpp"
 56 #include "utilities/xmlstream.hpp"
 57 
 58 TrainingData::TrainingDataSet TrainingData::_training_data_set(1024, 0x3fffffff);
 59 TrainingDataDictionary TrainingData::_archived_training_data_dictionary;
 60 TrainingDataDictionary TrainingData::_archived_training_data_dictionary_for_dumping;
 61 GrowableArrayCHeap<DumpTimeTrainingDataInfo, mtClassShared>* TrainingData::_dumptime_training_data_dictionary = nullptr;
 62 int TrainingData::TrainingDataLocker::_lock_mode;
 63 volatile bool TrainingData::TrainingDataLocker::_snapshot = false;
 64 
 65 MethodTrainingData::MethodTrainingData() {
 66   assert(CDSConfig::is_dumping_static_archive() || UseSharedSpaces, "only for CDS");
 67 }
 68 
 69 KlassTrainingData::KlassTrainingData() {
 70   assert(CDSConfig::is_dumping_static_archive() || UseSharedSpaces, "only for CDS");
 71 }
 72 
 73 CompileTrainingData::CompileTrainingData() : _level(-1), _compile_id(-1) {
 74   assert(CDSConfig::is_dumping_static_archive() || UseSharedSpaces, "only for CDS");
 75 }
 76 
 77 void TrainingData::initialize() {
 78   // this is a nop if training modes are not enabled
 79   if (have_data() || need_data()) {
 80     TrainingDataLocker::initialize();
 81   }
 82   RecompilationSchedule::initialize();
 83 }
 84 
 85 static void verify_archived_entry(TrainingData* td, const TrainingData::Key* k) {
 86   ResourceMark rm;
 87   guarantee(TrainingData::Key::can_compute_cds_hash(k), "%s %s " INTPTR_FORMAT,
 88             (k->name1() != nullptr ? k->name1()->as_C_string() : "(null)"),
 89             (k->name2() != nullptr ? k->name2()->as_C_string() : "(null)"),
 90             p2i(k->holder()));
 91 
 92   TrainingData* td1 = TrainingData::lookup_archived_training_data(k);
 93   guarantee(td == td1, "%s %s " INTPTR_FORMAT ": " INTPTR_FORMAT " != " INTPTR_FORMAT,
 94             (k->name1() != nullptr ? k->name1()->as_C_string() : "(null)"),
 95             (k->name2() != nullptr ? k->name2()->as_C_string() : "(null)"),
 96             p2i(k->holder()), p2i(td), p2i(td1));
 97 }
 98 
 99 void TrainingData::verify() {
100   if (TrainingData::have_data()) {
101     archived_training_data_dictionary()->iterate([&](TrainingData* td) {
102       if (td->is_KlassTrainingData()) {
103         KlassTrainingData* ktd = td->as_KlassTrainingData();
104         if (ktd->has_holder() && ktd->holder()->is_loaded()) {
105           Key k(ktd->holder());
106           verify_archived_entry(td, &k);
107         }
108         ktd->verify();
109       } else if (td->is_MethodTrainingData()) {
110         MethodTrainingData* mtd = td->as_MethodTrainingData();
111         if (mtd->has_holder() && mtd->holder()->method_holder()->is_loaded()) {
112           Key k(mtd);
113           verify_archived_entry(td, &k);
114         }
115         mtd->verify();
116       } else if (td->is_CompileTrainingData()) {
117         td->as_CompileTrainingData()->verify();
118       }
119     });
120   }
121 }
122 
123 
124 TrainingData::Key::Key(const KlassTrainingData* klass, Symbol* method_name, Symbol* signature)
125   : Key(method_name, signature, klass) {}
126 
127 TrainingData::Key::Key(const InstanceKlass* klass)
128   : Key(klass->name(), klass->class_loader_name_and_id()) {
129   // Often the loader is either null or the string "'app'" (w/ extra quotes).
130   // It can also be "'platform'".
131 }
132 
133 TrainingData::Key::Key(const Method* method, KlassTrainingData* holder)
134   : Key(method->name(), method->signature(), holder)
135 {
136   assert(method->method_holder() == holder->holder(), "mismatch");
137 }
138 
139 TrainingData::Key::Key(const MethodTrainingData* method) : Key(method->holder(), method->klass()) {}
140 
141 MethodTrainingData* MethodTrainingData::make(const methodHandle& method,
142                                              bool null_if_not_found) {
143   MethodTrainingData* mtd = nullptr;
144   if (!have_data() && !need_data()) {
145     return mtd;
146   }
147   // Try grabbing the cached value first.
148   MethodCounters* mcs = method->method_counters();
149   if (mcs != nullptr) {
150     mtd = mcs->method_training_data();
151     if (mtd != nullptr) {
152       return mtd;
153     }
154   } else {
155     mcs = Method::build_method_counters(Thread::current(), method());
156   }
157 
158   KlassTrainingData* holder = KlassTrainingData::make(method->method_holder(), null_if_not_found);
159   if (holder == nullptr) {
160     return nullptr; // allocation failure
161   }
162   Key key(method->name(), method->signature(), holder);
163   TrainingData* td = have_data()? lookup_archived_training_data(&key) : nullptr;
164   if (td != nullptr) {
165     mtd = td->as_MethodTrainingData();
166     method->init_training_data(mtd);  // Cache the pointer for next time.
167     return mtd;
168   } else {
169     TrainingDataLocker l;
170     td = training_data_set()->find(&key);
171     if (td == nullptr && null_if_not_found) {
172       return nullptr;
173     }
174     if (td != nullptr) {
175       mtd = td->as_MethodTrainingData();
176       method->init_training_data(mtd); // Cache the pointer for next time.
177       return mtd;
178     }
179   }
180   assert(td == nullptr && mtd == nullptr && !null_if_not_found, "Should return if have result");
181   KlassTrainingData* ktd = KlassTrainingData::make(method->method_holder());
182   if (ktd != nullptr) {
183     TrainingDataLocker l;
184     td = training_data_set()->find(&key);
185     if (td == nullptr) {
186       mtd = MethodTrainingData::allocate(method(), ktd);
187       if (mtd == nullptr) {
188         return nullptr; // allocation failure
189       }
190       td = training_data_set()->install(mtd);
191       assert(td == mtd, "");
192     } else {
193       mtd = td->as_MethodTrainingData();
194     }
195     method->init_training_data(mtd);
196   }
197   return mtd;
198 }
199 
200 void MethodTrainingData::print_on(outputStream* st, bool name_only) const {
201   _klass->print_on(st, true);
202   st->print(".");
203   name()->print_symbol_on(st);
204   signature()->print_symbol_on(st);
205   if (name_only) {
206     return;
207   }
208   if (!has_holder()) {
209     st->print("[SYM]");
210   }
211   if (_level_mask) {
212     st->print(" LM%d", _level_mask);
213   }
214   st->print(" mc=%p mdo=%p", _final_counters, _final_profile);
215 }
216 
217 CompileTrainingData* CompileTrainingData::make(CompileTask* task) {
218   int level = task->comp_level();
219   int compile_id = task->compile_id();
220   Thread* thread = Thread::current();
221   methodHandle m(thread, task->method());
222   MethodTrainingData* mtd = MethodTrainingData::make(m);
223   if (mtd == nullptr) {
224     return nullptr; // allocation failure
225   }
226   mtd->notice_compilation(level);
227 
228   TrainingDataLocker l;
229   CompileTrainingData* ctd = CompileTrainingData::allocate(mtd, level, compile_id);
230   if (ctd != nullptr) {
231     if (mtd->_last_toplevel_compiles[level - 1] != nullptr) {
232       if (mtd->_last_toplevel_compiles[level - 1]->compile_id() < compile_id) {
233         mtd->_last_toplevel_compiles[level - 1]->clear_init_deps();
234         mtd->_last_toplevel_compiles[level - 1] = ctd;
235         mtd->_highest_top_level = MAX2(mtd->_highest_top_level, level);
236       }
237     } else {
238       mtd->_last_toplevel_compiles[level - 1] = ctd;
239       mtd->_highest_top_level = MAX2(mtd->_highest_top_level, level);
240     }
241   }
242   return ctd;
243 }
244 
245 
246 void CompileTrainingData::dec_init_deps_left(KlassTrainingData* ktd) {
247   LogStreamHandle(Trace, training) log;
248   if (log.is_enabled()) {
249     log.print("CTD "); print_on(&log); log.cr();
250     log.print("KTD "); ktd->print_on(&log); log.cr();
251   }
252   assert(ktd!= nullptr && ktd->has_holder(), "");
253   assert(_init_deps.contains(ktd), "");
254   assert(_init_deps_left > 0, "");
255 
256   uint init_deps_left1 = Atomic::sub(&_init_deps_left, 1);
257 
258   if (log.is_enabled()) {
259     uint init_deps_left2 = compute_init_deps_left();
260     log.print("init_deps_left: %d (%d)", init_deps_left1, init_deps_left2);
261     ktd->print_on(&log, true);
262   }
263 }
264 
265 uint CompileTrainingData::compute_init_deps_left(bool count_initialized) {
266   int left = 0;
267   for (int i = 0; i < _init_deps.length(); i++) {
268     KlassTrainingData* ktd = _init_deps.at(i);
269     // Ignore symbolic refs and already initialized classes (unless explicitly requested).
270     if (ktd->has_holder()) {
271       InstanceKlass* holder = ktd->holder();
272       if (!ktd->holder()->is_initialized() || count_initialized) {
273         ++left;
274       } else if (holder->is_shared_unregistered_class()) {
275         Key k(holder);
276         if (!Key::can_compute_cds_hash(&k)) {
277           ++left; // FIXME: !!! init tracking doesn't work well for custom loaders !!!
278         }
279       }
280     }
281   }
282   return left;
283 }
284 
285 void CompileTrainingData::print_on(outputStream* st, bool name_only) const {
286   _method->print_on(st, true);
287   st->print("#%dL%d", _compile_id, _level);
288   if (name_only) {
289     return;
290   }
291   #define MAYBE_TIME(Q, _qtime) \
292     if (_qtime != 0) st->print(" " #Q "%.3f", _qtime)
293   MAYBE_TIME(Q, _qtime);
294   MAYBE_TIME(S, _stime);
295   MAYBE_TIME(E, _etime);
296   if (_init_deps.length() > 0) {
297     if (_init_deps_left > 0) {
298       st->print(" udeps=%d", _init_deps_left);
299     }
300     for (int i = 0, len = _init_deps.length(); i < len; i++) {
301       st->print(" dep:");
302       _init_deps.at(i)->print_on(st, true);
303     }
304   }
305 }
306 
307 void CompileTrainingData::record_compilation_queued(CompileTask* task) {
308   _qtime = tty->time_stamp().seconds();
309 }
310 void CompileTrainingData::record_compilation_start(CompileTask* task) {
311   _stime = tty->time_stamp().seconds();
312 }
313 void CompileTrainingData::record_compilation_end(CompileTask* task) {
314   _etime = tty->time_stamp().seconds();
315   if (task->is_success()) {   // record something about the nmethod output
316     _nm_total_size = task->nm_total_size();
317   }
318 }
319 void CompileTrainingData::notice_inlined_method(CompileTask* task,
320                                                 const methodHandle& method) {
321   MethodTrainingData* mtd = MethodTrainingData::make(method);
322   if (mtd != nullptr) {
323     mtd->notice_compilation(task->comp_level(), true);
324   }
325 }
326 
327 void CompileTrainingData::notice_jit_observation(ciEnv* env, ciBaseObject* what) {
328   // A JIT is starting to look at class k.
329   // We could follow the queries that it is making, but it is
330   // simpler to assume, conservatively, that the JIT will
331   // eventually depend on the initialization state of k.
332   CompileTask* task = env->task();
333   assert(task != nullptr, "");
334   Method* method = task->method();
335   InstanceKlass* compiling_klass = method->method_holder();
336   if (what->is_metadata()) {
337     ciMetadata* md = what->as_metadata();
338     if (md->is_loaded() && md->is_instance_klass()) {
339       ciInstanceKlass* cik = md->as_instance_klass();
340 
341       if (cik->is_initialized()) {
342         InstanceKlass* ik = md->as_instance_klass()->get_instanceKlass();
343         KlassTrainingData* ktd = KlassTrainingData::make(ik);
344         if (ktd == nullptr) {
345           // Allocation failure or snapshot in progress
346           return;
347         }
348         // This JIT task is (probably) requesting that ik be initialized,
349         // so add him to my _init_deps list.
350         TrainingDataLocker l;
351         add_init_dep(ktd);
352       }
353     }
354   }
355 }
356 
357 void KlassTrainingData::prepare(Visitor& visitor) {
358   if (visitor.is_visited(this)) {
359     return;
360   }
361   visitor.visit(this);
362   ClassLoaderData* loader_data = nullptr;
363   if (_holder != nullptr) {
364     loader_data = _holder->class_loader_data();
365   } else {
366     loader_data = java_lang_ClassLoader::loader_data(SystemDictionary::java_system_loader()); // default CLD
367   }
368   _comp_deps.prepare(loader_data);
369 }
370 
371 void MethodTrainingData::prepare(Visitor& visitor) {
372   if (visitor.is_visited(this)) {
373     return;
374   }
375   visitor.visit(this);
376   klass()->prepare(visitor);
377   if (has_holder()) {
378     _final_counters = holder()->method_counters();
379     _final_profile  = holder()->method_data();
380     assert(_final_profile == nullptr || _final_profile->method() == holder(), "");
381   }
382   for (int i = 0; i < CompLevel_count; i++) {
383     CompileTrainingData* ctd = _last_toplevel_compiles[i];
384     if (ctd != nullptr) {
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 KlassTrainingData* KlassTrainingData::make(InstanceKlass* holder, bool null_if_not_found) {
402   Key key(holder);
403   TrainingData* td = have_data() ? lookup_archived_training_data(&key) : nullptr;
404   KlassTrainingData* ktd = nullptr;
405   if (td != nullptr) {
406     ktd = td->as_KlassTrainingData();
407     guarantee(!ktd->has_holder() || ktd->holder() == holder, "");
408     if (ktd->has_holder()) {
409       return ktd;
410     }
411   }
412   TrainingDataLocker l;
413   td = training_data_set()->find(&key);
414   if (td == nullptr) {
415     if (null_if_not_found) {
416       return nullptr;
417     }
418     ktd = KlassTrainingData::allocate(holder);
419     if (ktd == nullptr) {
420       return nullptr; // allocation failure
421     }
422     td = training_data_set()->install(ktd);
423     assert(ktd == td, "");
424   } else {
425     ktd = td->as_KlassTrainingData();
426     guarantee(ktd->holder() != nullptr, "null holder");
427   }
428   assert(ktd != nullptr, "");
429   guarantee(ktd->holder() == holder, "");
430   return ktd;
431 }
432 
433 void KlassTrainingData::print_on(outputStream* st, bool name_only) const {
434   name()->print_symbol_on(st);
435   if (has_holder()) {
436     switch (holder()->init_state()) {
437       case InstanceKlass::allocated:            st->print("[A]"); break;
438       case InstanceKlass::loaded:               st->print("[D]"); break;
439       case InstanceKlass::linked:               st->print("[L]"); break;
440       case InstanceKlass::being_initialized:    st->print("[i]"); break;
441       case InstanceKlass::fully_initialized:    /*st->print("");*/ break;
442       case InstanceKlass::initialization_error: st->print("[E]"); break;
443       default: fatal("unknown state: %d", holder()->init_state());
444     }
445     if (holder()->is_interface()) {
446       st->print("I");
447     }
448   } else {
449     st->print("[SYM]");
450   }
451   if (name_only) {
452     return;
453   }
454   if (_comp_deps.length() > 0) {
455     for (int i = 0, len = _comp_deps.length(); i < len; i++) {
456       st->print(" dep:");
457       _comp_deps.at(i)->print_on(st, true);
458     }
459   }
460 }
461 
462 KlassTrainingData::KlassTrainingData(InstanceKlass* klass) : TrainingData(klass) {
463   if (holder() == klass) {
464     return;   // no change to make
465   }
466 
467   jobject hmj = _holder_mirror;
468   if (hmj != nullptr) {   // clear out previous handle, if any
469     _holder_mirror = nullptr;
470     assert(JNIHandles::is_global_handle(hmj), "");
471     JNIHandles::destroy_global(hmj);
472   }
473 
474   // Keep the klass alive during the training run, unconditionally.
475   //
476   // FIXME: Revisit this decision; we could allow training runs to
477   // unload classes in the normal way.  We might use make_weak_global
478   // instead of make_global.
479   //
480   // The data from the training run would mention the name of the
481   // unloaded class (and of its loader).  Is it worth the complexity
482   // to track and then unload classes, remembering just their names?
483 
484   if (klass != nullptr) {
485     Handle hm(JavaThread::current(), klass->java_mirror());
486     hmj = JNIHandles::make_global(hm);
487     Atomic::release_store(&_holder_mirror, hmj);
488   }
489 
490   Atomic::release_store(&_holder, const_cast<InstanceKlass*>(klass));
491   assert(holder() == klass, "");
492 }
493 
494 void KlassTrainingData::notice_fully_initialized() {
495   ResourceMark rm;
496   assert(has_holder(), "");
497   assert(holder()->is_initialized(), "wrong state: %s %s",
498          holder()->name()->as_C_string(), holder()->init_state_name());
499 
500   TrainingDataLocker l; // Not a real lock if we don't collect the data,
501                         // that's why we need the atomic decrement below.
502   for (int i = 0; i < comp_dep_count(); i++) {
503     comp_dep(i)->dec_init_deps_left(this);
504   }
505   holder()->set_has_init_deps_processed();
506 }
507 
508 void TrainingData::init_dumptime_table(TRAPS) {
509   if (!need_data()) {
510     return;
511   }
512   _dumptime_training_data_dictionary = new GrowableArrayCHeap<DumpTimeTrainingDataInfo, mtClassShared>();
513   if (CDSConfig::is_dumping_final_static_archive()) {
514     _archived_training_data_dictionary.iterate([&](TrainingData* record) {
515       _dumptime_training_data_dictionary->append(record);
516     });
517   } else {
518     TrainingDataLocker l;
519     TrainingDataLocker::snapshot();
520 
521     ResourceMark rm;
522     Visitor visitor(training_data_set()->size());
523     training_data_set()->iterate_all([&](const TrainingData::Key* k, TrainingData* td) {
524       td->prepare(visitor);
525       if (!td->is_CompileTrainingData()) {
526         _dumptime_training_data_dictionary->append(td);
527       }
528     });
529 
530     if (VerifyTrainingData) {
531       training_data_set()->verify();
532     }
533   }
534 
535   RecompilationSchedule::prepare(CHECK);
536 }
537 
538 #if INCLUDE_CDS
539 void TrainingData::iterate_roots(MetaspaceClosure* it) {
540   if (!need_data()) {
541     return;
542   }
543   assert(_dumptime_training_data_dictionary != nullptr, "");
544   for (int i = 0; i < _dumptime_training_data_dictionary->length(); i++) {
545     _dumptime_training_data_dictionary->at(i).metaspace_pointers_do(it);
546   }
547   RecompilationSchedule::iterate_roots(it);
548 }
549 
550 void TrainingData::dump_training_data() {
551   if (!need_data()) {
552     return;
553   }
554   write_training_data_dictionary(&_archived_training_data_dictionary_for_dumping);
555 }
556 
557 void TrainingData::cleanup_training_data() {
558   if (_dumptime_training_data_dictionary != nullptr) {
559     ResourceMark rm;
560     Visitor visitor(_dumptime_training_data_dictionary->length());
561     for (int i = 0; i < _dumptime_training_data_dictionary->length(); i++) {
562       TrainingData* td = _dumptime_training_data_dictionary->at(i).training_data();
563       td->cleanup(visitor);
564     }
565   }
566   RecompilationSchedule::cleanup();
567 }
568 
569 void KlassTrainingData::cleanup(Visitor& visitor) {
570   if (visitor.is_visited(this)) {
571     return;
572   }
573   visitor.visit(this);
574   if (holder() != nullptr) {
575     bool is_excluded = !holder()->is_loaded() || SystemDictionaryShared::check_for_exclusion(holder(), nullptr);
576     if (is_excluded) {
577       ResourceMark rm;
578       log_debug(cds)("Cleanup KTD %s", name()->as_klass_external_name());
579       _holder = nullptr; // reset
580     }
581   }
582   for (int i = 0; i < _comp_deps.length(); i++) {
583     _comp_deps.at(i)->cleanup(visitor);
584   }
585 }
586 
587 void MethodTrainingData::cleanup(Visitor& visitor) {
588   if (visitor.is_visited(this)) {
589     return;
590   }
591   visitor.visit(this);
592   if (has_holder()) {
593     if (SystemDictionaryShared::check_for_exclusion(holder()->method_holder(), nullptr)) {
594       log_debug(cds)("Cleanup MTD %s::%s", name()->as_klass_external_name(), signature()->as_utf8());
595       if (_final_profile != nullptr && _final_profile->method() != _holder) {
596         // FIXME: MDO still points at the stale method; either completely drop the MDO or zero out the link
597         log_warning(cds)("Stale MDO for  %s::%s", name()->as_klass_external_name(), signature()->as_utf8());
598       }
599       _holder = nullptr;
600     }
601   }
602   for (int i = 0; i < CompLevel_count; i++) {
603     CompileTrainingData* ctd = _last_toplevel_compiles[i];
604     if (ctd != nullptr) {
605       ctd->cleanup(visitor);
606     }
607   }
608 }
609 
610 void KlassTrainingData::verify() {
611   for (int i = 0; i < comp_dep_count(); i++) {
612     CompileTrainingData* ctd = comp_dep(i);
613     if (!ctd->_init_deps.contains(this)) {
614       print_on(tty); tty->cr();
615       ctd->print_on(tty); tty->cr();
616     }
617     guarantee(ctd->_init_deps.contains(this), "");
618   }
619 }
620 
621 void MethodTrainingData::verify() {
622   iterate_all_compiles([](CompileTrainingData* ctd) {
623     ctd->verify();
624 
625     int init_deps_left1 = ctd->init_deps_left();
626     int init_deps_left2 = ctd->compute_init_deps_left();
627 
628     if (init_deps_left1 != init_deps_left2) {
629       ctd->print_on(tty); tty->cr();
630     }
631     guarantee(init_deps_left1 == init_deps_left2, "mismatch: %d %d %d",
632               init_deps_left1, init_deps_left2, ctd->init_deps_left());
633   });
634 }
635 
636 void CompileTrainingData::verify() {
637   for (int i = 0; i < init_dep_count(); i++) {
638     KlassTrainingData* ktd = init_dep(i);
639     if (ktd->has_holder() && ktd->holder()->is_shared_unregistered_class()) {
640       LogStreamHandle(Warning, training) log;
641       if (log.is_enabled()) {
642         ResourceMark rm;
643         log.print("CTD "); print_value_on(&log);
644         log.print(" depends on unregistered class %s", ktd->holder()->name()->as_C_string());
645       }
646     }
647     if (!ktd->_comp_deps.contains(this)) {
648       print_on(tty); tty->cr();
649       ktd->print_on(tty); tty->cr();
650     }
651     guarantee(ktd->_comp_deps.contains(this), "");
652   }
653 }
654 
655 void CompileTrainingData::cleanup(Visitor& visitor) {
656   if (visitor.is_visited(this)) {
657     return;
658   }
659   visitor.visit(this);
660   method()->cleanup(visitor);
661 }
662 
663 void TrainingData::serialize_training_data(SerializeClosure* soc) {
664   if (soc->writing()) {
665     _archived_training_data_dictionary_for_dumping.serialize_header(soc);
666   } else {
667     _archived_training_data_dictionary.serialize_header(soc);
668   }
669   RecompilationSchedule::serialize_training_data(soc);
670 }
671 
672 void TrainingData::print_archived_training_data_on(outputStream* st) {
673   st->print_cr("Archived TrainingData Dictionary");
674   TrainingDataPrinter tdp(st);
675   TrainingDataLocker::initialize();
676   _archived_training_data_dictionary.iterate(&tdp);
677   RecompilationSchedule::print_archived_training_data_on(st);
678 }
679 
680 void TrainingData::Key::metaspace_pointers_do(MetaspaceClosure *iter) {
681   iter->push(&_name1);
682   iter->push(&_name2);
683   iter->push((TrainingData**)&_holder);
684 }
685 
686 void TrainingData::metaspace_pointers_do(MetaspaceClosure* iter) {
687   _key.metaspace_pointers_do(iter);
688 }
689 
690 bool TrainingData::Key::can_compute_cds_hash(const Key* const& k) {
691   return (k->name1() == nullptr || MetaspaceObj::is_shared(k->name1())) &&
692          (k->name2() == nullptr || MetaspaceObj::is_shared(k->name2())) &&
693          (k->holder() == nullptr || MetaspaceObj::is_shared(k->holder()));
694 }
695 
696 uint TrainingData::Key::cds_hash(const Key* const& k) {
697   uint hash = 0;
698   if (k->holder() != nullptr) {
699     hash += SystemDictionaryShared::hash_for_shared_dictionary((address)k->holder());
700   }
701   if (k->name1() != nullptr) {
702     hash += SystemDictionaryShared::hash_for_shared_dictionary((address)k->name1());
703   }
704   if (k->name2() != nullptr) {
705     hash += SystemDictionaryShared::hash_for_shared_dictionary((address)k->name2());
706   }
707   return hash;
708 }
709 
710 void TrainingData::write_training_data_dictionary(TrainingDataDictionary* dictionary) {
711   if (!need_data()) {
712     return;
713   }
714   assert(_dumptime_training_data_dictionary != nullptr, "");
715   CompactHashtableStats stats;
716   dictionary->reset();
717   CompactHashtableWriter writer(_dumptime_training_data_dictionary->length(), &stats);
718   for (int i = 0; i < _dumptime_training_data_dictionary->length(); i++) {
719     TrainingData* td = _dumptime_training_data_dictionary->at(i).training_data();
720 #ifdef ASSERT
721     for (int j = i+1; j < _dumptime_training_data_dictionary->length(); j++) {
722       TrainingData* td1 = _dumptime_training_data_dictionary->at(j).training_data();
723       assert(!TrainingData::Key::equals(td1, td->key(), -1), "conflict");
724     }
725 #endif // ASSERT
726     td = ArchiveBuilder::current()->get_buffered_addr(td);
727     uint hash = TrainingData::Key::cds_hash(td->key());
728     u4 delta = ArchiveBuilder::current()->buffer_to_offset_u4((address)td);
729     writer.add(hash, delta);
730   }
731   writer.dump(dictionary, "training data dictionary");
732 }
733 
734 size_t TrainingData::estimate_size_for_archive() {
735   if (_dumptime_training_data_dictionary != nullptr) {
736     return CompactHashtableWriter::estimate_size(_dumptime_training_data_dictionary->length());
737   } else {
738     return 0;
739   }
740 }
741 
742 TrainingData* TrainingData::lookup_archived_training_data(const Key* k) {
743   // For this to work, all components of the key must be in shared metaspace.
744   if (!TrainingData::Key::can_compute_cds_hash(k) || _archived_training_data_dictionary.empty()) {
745     return nullptr;
746   }
747   uint hash = TrainingData::Key::cds_hash(k);
748   TrainingData* td = _archived_training_data_dictionary.lookup(k, hash, -1 /*unused*/);
749   if (td != nullptr) {
750     if ((td->is_KlassTrainingData()  && td->as_KlassTrainingData()->has_holder()) ||
751         (td->is_MethodTrainingData() && td->as_MethodTrainingData()->has_holder())) {
752       return td;
753     } else {
754       // FIXME: decide what to do with symbolic TD
755       LogStreamHandle(Trace, training) log;
756       if (log.is_enabled()) {
757         ResourceMark rm;
758         log.print_cr("Ignored symbolic TrainingData:");
759         log.print_cr("  Key: %s %s",
760                      (k->name1() != nullptr ? k->name1()->as_C_string() : "(null)"),
761                      (k->name2() != nullptr ? k->name2()->as_C_string() : "(null)"));
762         td->print_on(&log);
763         log.cr();
764       }
765     }
766   }
767   return nullptr;
768 }
769 #endif
770 
771 KlassTrainingData* TrainingData::lookup_for(InstanceKlass* ik) {
772   if (TrainingData::have_data() && ik != nullptr && ik->is_loaded()) {
773     TrainingData::Key key(ik);
774     TrainingData* td = TrainingData::lookup_archived_training_data(&key);
775     if (td != nullptr && td->is_KlassTrainingData()) {
776       return td->as_KlassTrainingData();
777     }
778   }
779   return nullptr;
780 }
781 
782 MethodTrainingData* TrainingData::lookup_for(Method* m) {
783   if (TrainingData::have_data() && m != nullptr) {
784     KlassTrainingData* holder_ktd = TrainingData::lookup_for(m->method_holder());
785     if (holder_ktd != nullptr) {
786       TrainingData::Key key(m->name(), m->signature(), holder_ktd);
787       TrainingData* td = TrainingData::lookup_archived_training_data(&key);
788       if (td != nullptr && td->is_MethodTrainingData()) {
789         return td->as_MethodTrainingData();
790       }
791     }
792   }
793   return nullptr;
794 }
795 
796 template <typename T>
797 void TrainingData::DepList<T>::metaspace_pointers_do(MetaspaceClosure* iter) {
798   iter->push(&_deps);
799 }
800 
801 void KlassTrainingData::metaspace_pointers_do(MetaspaceClosure* iter) {
802   log_trace(cds)("Iter(KlassTrainingData): %p", this);
803   TrainingData::metaspace_pointers_do(iter);
804   _comp_deps.metaspace_pointers_do(iter);
805   iter->push(&_holder);
806 }
807 
808 void MethodTrainingData::metaspace_pointers_do(MetaspaceClosure* iter) {
809   log_trace(cds)("Iter(MethodTrainingData): %p", this);
810   TrainingData::metaspace_pointers_do(iter);
811   iter->push(&_klass);
812   iter->push((Method**)&_holder);
813   for (int i = 0; i < CompLevel_count; i++) {
814     iter->push(&_last_toplevel_compiles[i]);
815   }
816   iter->push(&_final_profile);
817   iter->push(&_final_counters);
818 }
819 
820 void CompileTrainingData::metaspace_pointers_do(MetaspaceClosure* iter) {
821   log_trace(cds)("Iter(CompileTrainingData): %p", this);
822   TrainingData::metaspace_pointers_do(iter);
823   _init_deps.metaspace_pointers_do(iter);
824   _ci_records.metaspace_pointers_do(iter);
825   iter->push(&_method);
826 }
827 
828 template <typename T>
829 void TrainingData::DepList<T>::prepare(ClassLoaderData* loader_data) {
830   if (_deps == nullptr && _deps_dyn != nullptr) {
831     int len = _deps_dyn->length();
832     _deps = MetadataFactory::new_array_from_c_heap<T>(len, mtClassShared);
833     for (int i = 0; i < len; i++) {
834       _deps->at_put(i, _deps_dyn->at(i)); // copy
835     }
836   }
837 }
838 
839 KlassTrainingData* KlassTrainingData::allocate(InstanceKlass* holder) {
840   assert(need_data() || have_data(), "");
841   if (TrainingDataLocker::can_add()) {
842     return new (mtClassShared) KlassTrainingData(holder);
843   }
844   return nullptr;
845 }
846 
847 MethodTrainingData* MethodTrainingData::allocate(Method* m, KlassTrainingData* ktd) {
848   assert(need_data() || have_data(), "");
849   if (TrainingDataLocker::can_add()) {
850     return new (mtClassShared) MethodTrainingData(m, ktd, m->name(), m->signature());
851   }
852   return nullptr;
853 }
854 
855 CompileTrainingData* CompileTrainingData::allocate(MethodTrainingData* mtd, int level, int compile_id) {
856   assert(need_data() || have_data(), "");
857   if (TrainingDataLocker::can_add()) {
858     return new (mtClassShared) CompileTrainingData(mtd, level, compile_id);
859   }
860   return nullptr;
861 }
862 
863 void TrainingDataPrinter::do_value(TrainingData* td) {
864 #ifdef ASSERT
865   TrainingData::Key key(td->key()->name1(), td->key()->name2(), td->key()->holder());
866   assert(td == TrainingData::archived_training_data_dictionary()->lookup(td->key(), TrainingData::Key::cds_hash(td->key()), -1), "");
867   assert(td == TrainingData::archived_training_data_dictionary()->lookup(&key, TrainingData::Key::cds_hash(&key), -1), "");
868 #endif // ASSERT
869 
870   const char* type = (td->is_KlassTrainingData()   ? "K" :
871                       td->is_MethodTrainingData()  ? "M" :
872                       td->is_CompileTrainingData() ? "C" : "?");
873   _st->print("%4d: %p %s ", _index++, td, type);
874   td->print_on(_st);
875   _st->cr();
876   if (td->is_KlassTrainingData()) {
877     td->as_KlassTrainingData()->iterate_all_comp_deps([&](CompileTrainingData* ctd) {
878       ResourceMark rm;
879       _st->print_raw("  C ");
880       ctd->print_on(_st);
881       _st->cr();
882     });
883   } else if (td->is_MethodTrainingData()) {
884     td->as_MethodTrainingData()->iterate_all_compiles([&](CompileTrainingData* ctd) {
885       ResourceMark rm;
886       _st->print_raw("  C ");
887       ctd->print_on(_st);
888       _st->cr();
889     });
890   } else if (td->is_CompileTrainingData()) {
891     // ?
892   }
893 }
894 
895 
896 #if INCLUDE_CDS
897 void KlassTrainingData::remove_unshareable_info() {
898   TrainingData::remove_unshareable_info();
899   _holder_mirror = nullptr;
900   _comp_deps.remove_unshareable_info();
901 }
902 
903 void MethodTrainingData::remove_unshareable_info() {
904   TrainingData::remove_unshareable_info();
905   if (_final_counters != nullptr) {
906     _final_counters->remove_unshareable_info();
907   }
908   if (_final_profile != nullptr) {
909     _final_profile->remove_unshareable_info();
910   }
911 }
912 
913 void CompileTrainingData::remove_unshareable_info() {
914   TrainingData::remove_unshareable_info();
915   _init_deps.remove_unshareable_info();
916   _ci_records.remove_unshareable_info();
917   _init_deps_left = compute_init_deps_left(true);
918 }
919 
920 #endif // INCLUDE_CDS