1 /*
2 * Copyright (c) 2025, 2026, 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/aotCompressedPointers.hpp"
26 #include "cds/cdsConfig.hpp"
27 #include "ci/ciEnv.hpp"
28 #include "ci/ciMetadata.hpp"
29 #include "classfile/compactHashtable.hpp"
30 #include "classfile/javaClasses.hpp"
31 #include "classfile/symbolTable.hpp"
32 #include "classfile/systemDictionaryShared.hpp"
33 #include "compiler/compileTask.hpp"
34 #include "memory/metadataFactory.hpp"
35 #include "memory/metaspaceClosure.hpp"
36 #include "memory/resourceArea.hpp"
37 #include "memory/universe.hpp"
38 #include "oops/method.hpp"
39 #include "oops/method.inline.hpp"
40 #include "oops/methodCounters.hpp"
41 #include "oops/recompilationSchedule.hpp"
42 #include "oops/trainingData.hpp"
43 #include "runtime/arguments.hpp"
44 #include "runtime/javaThread.inline.hpp"
45 #include "runtime/jniHandles.inline.hpp"
46 #include "utilities/growableArray.hpp"
47
48 TrainingData::TrainingDataSet TrainingData::_training_data_set(1024, 0x3fffffff);
49 TrainingData::TrainingDataDictionary TrainingData::_archived_training_data_dictionary;
50 TrainingData::TrainingDataDictionary TrainingData::_archived_training_data_dictionary_for_dumping;
51 TrainingData::DumptimeTrainingDataDictionary* TrainingData::_dumptime_training_data_dictionary = nullptr;
52 int TrainingData::TrainingDataLocker::_lock_mode;
53 volatile bool TrainingData::TrainingDataLocker::_snapshot = false;
54
55 MethodTrainingData::MethodTrainingData() {
56 // Used by cppVtables.cpp only
57 assert(CDSConfig::is_dumping_static_archive() || UseSharedSpaces, "only for CDS");
58 }
59
60 KlassTrainingData::KlassTrainingData() {
61 // Used by cppVtables.cpp only
62 assert(CDSConfig::is_dumping_static_archive() || UseSharedSpaces, "only for CDS");
63 }
64
65 CompileTrainingData::CompileTrainingData() : _level(-1), _compile_id(-1) {
66 // Used by cppVtables.cpp only
67 assert(CDSConfig::is_dumping_static_archive() || UseSharedSpaces, "only for CDS");
68 }
69
70 void TrainingData::initialize() {
71 // this is a nop if training modes are not enabled
72 if (have_data() || need_data()) {
73 TrainingDataLocker::initialize();
74 }
75 RecompilationSchedule::initialize();
76 if (have_data() && need_data()) {
77 TrainingDataLocker l;
78 archived_training_data_dictionary()->iterate_all([&](TrainingData* td) {
79 training_data_set()->install(td);
80 });
81 }
82 }
83
84 static void verify_archived_entry(TrainingData* td, const TrainingData::Key* k) {
85 guarantee(TrainingData::Key::can_compute_cds_hash(k), "");
86 TrainingData* td1 = TrainingData::lookup_archived_training_data(k);
87 guarantee(td == td1, "");
88 }
89
90 void TrainingData::verify() {
91 if (have_data() && !need_data() && !assembling_data()) {
92 archived_training_data_dictionary()->iterate_all([&](TrainingData* td) {
93 if (td->is_KlassTrainingData()) {
94 KlassTrainingData* ktd = td->as_KlassTrainingData();
95 if (ktd->has_holder() && ktd->holder()->is_loaded()) {
96 Key k(ktd->holder());
97 verify_archived_entry(td, &k);
98 }
99 ktd->verify();
100 } else if (td->is_MethodTrainingData()) {
101 MethodTrainingData* mtd = td->as_MethodTrainingData();
102 if (mtd->has_holder() && mtd->holder()->method_holder()->is_loaded()) {
103 Key k(mtd->holder());
104 verify_archived_entry(td, &k);
105 }
106 mtd->verify(/*verify_dep_counter*/true);
107 }
108 });
109 }
110 if (need_data()) {
111 TrainingDataLocker l;
112 training_data_set()->iterate([&](TrainingData* td) {
113 if (td->is_KlassTrainingData()) {
114 KlassTrainingData* ktd = td->as_KlassTrainingData();
115 ktd->verify();
116 } else if (td->is_MethodTrainingData()) {
117 MethodTrainingData* mtd = td->as_MethodTrainingData();
118 // During the training run init deps tracking is not setup yet,
119 // don't verify it.
120 mtd->verify(/*verify_dep_counter*/false);
121 }
122 });
123 }
124 }
125
126 MethodTrainingData* MethodTrainingData::make(const methodHandle& method, bool null_if_not_found, bool use_cache) {
127 MethodTrainingData* mtd = nullptr;
128 if (!have_data() && !need_data()) {
129 return mtd;
130 }
131 // Try grabbing the cached value first.
132 // Cache value is stored in MethodCounters and the following are the
133 // possible states:
134 // 1. Cached value is method_training_data_sentinel().
135 // This is an initial state and needs a full lookup.
136 // 2. Cached value is null.
137 // Lookup failed the last time, if we don't plan to create a new TD object,
138 // i.e. null_if_no_found == true, then just return a null.
139 // 3. Cache value is not null.
140 // Return it, the value of training_data_lookup_failed doesn't matter.
141 MethodCounters* mcs = method->method_counters();
142 if (mcs != nullptr) {
143 mtd = mcs->method_training_data();
144 if (mtd != nullptr && mtd != mcs->method_training_data_sentinel()) {
145 return mtd;
146 }
147 if (null_if_not_found && mtd == nullptr) {
148 assert(mtd == nullptr, "No training data found");
149 return nullptr;
150 }
151 } else if (use_cache) {
152 mcs = Method::build_method_counters(Thread::current(), method());
153 }
154
155 TrainingData* td = nullptr;
156
157 Key key(method());
158 if (have_data() && !need_data()) {
159 td = lookup_archived_training_data(&key);
160 if (td != nullptr) {
161 mtd = td->as_MethodTrainingData();
162 } else {
163 mtd = nullptr;
164 }
165 // Cache the pointer to MTD in MethodCounters for faster lookup (could be null if not found)
166 method->init_training_data(mtd);
167 }
168
169 if (need_data()) {
170 TrainingDataLocker l;
171 td = training_data_set()->find(&key);
172 if (td == nullptr) {
173 if (!null_if_not_found) {
174 KlassTrainingData* ktd = KlassTrainingData::make(method->method_holder());
175 if (ktd == nullptr) {
176 return nullptr; // allocation failure
177 }
178 mtd = MethodTrainingData::allocate(method(), ktd);
179 if (mtd == nullptr) {
180 return nullptr; // allocation failure
181 }
182 td = training_data_set()->install(mtd);
183 assert(td == mtd, "");
184 } else {
185 mtd = nullptr;
186 }
187 } else {
188 mtd = td->as_MethodTrainingData();
189 }
190 // Cache the pointer to MTD in MethodCounters for faster lookup (could be null if not found)
191 method->init_training_data(mtd);
192 }
193
194 return mtd;
195 }
196
197 void MethodTrainingData::print_on(outputStream* st, bool name_only) const {
198 if (has_holder()) {
199 _klass->print_on(st, true);
200 st->print(".");
201 name()->print_symbol_on(st);
202 signature()->print_symbol_on(st);
203 }
204 if (name_only) {
205 return;
206 }
207 if (!has_holder()) {
208 st->print("[SYM]");
209 }
210 if (_level_mask) {
211 st->print(" LM%d", _level_mask);
212 }
213 st->print(" mc=%p mdo=%p", _final_counters, _final_profile);
214 }
215
216 CompileTrainingData* CompileTrainingData::make(CompileTask* task) {
217 int level = task->comp_level();
218 int compile_id = task->compile_id();
219 Thread* thread = Thread::current();
220 methodHandle m(thread, task->method());
221 if (m->method_holder() == nullptr) {
222 return nullptr; // do not record (dynamically generated method)
223 }
224 MethodTrainingData* mtd = MethodTrainingData::make(m);
225 if (mtd == nullptr) {
226 return nullptr; // allocation failure
227 }
228 mtd->notice_compilation(level);
229
230 TrainingDataLocker l;
231 CompileTrainingData* ctd = CompileTrainingData::allocate(mtd, level, compile_id);
232 if (ctd != nullptr) {
233 CompileTrainingData*& last_ctd = mtd->_last_toplevel_compiles[level - 1];
234 if (last_ctd != nullptr) {
235 assert(mtd->highest_top_level() >= level, "consistency");
236 if (last_ctd->compile_id() < compile_id) {
237 last_ctd->clear_init_deps();
238 last_ctd = ctd;
239 }
240 } else {
241 last_ctd = ctd;
242 mtd->notice_toplevel_compilation(level);
243 }
244 }
245 return ctd;
246 }
247
248
249 void CompileTrainingData::dec_init_deps_left_release(KlassTrainingData* ktd) {
250 LogStreamHandle(Trace, training) log;
251 if (log.is_enabled()) {
252 log.print("CTD "); print_on(&log); log.cr();
253 log.print("KTD "); ktd->print_on(&log); log.cr();
254 }
255 assert(ktd!= nullptr && ktd->has_holder(), "");
256 assert(_init_deps.contains(ktd), "");
257 assert(_init_deps_left > 0, "");
258
259 uint init_deps_left1 = AtomicAccess::sub(&_init_deps_left, 1);
260
261 if (log.is_enabled()) {
262 uint init_deps_left2 = compute_init_deps_left();
263 log.print("init_deps_left: %d (%d)", init_deps_left1, init_deps_left2);
264 ktd->print_on(&log, true);
265 }
266 }
267
268 uint CompileTrainingData::compute_init_deps_left(bool count_initialized) {
269 int left = 0;
270 for (int i = 0; i < _init_deps.length(); i++) {
271 KlassTrainingData* ktd = _init_deps.at(i);
272 // Ignore symbolic refs and already initialized classes (unless explicitly requested).
273 if (ktd->has_holder()) {
274 InstanceKlass* holder = ktd->holder();
275 if (!ktd->holder()->is_initialized() || count_initialized) {
276 ++left;
277 } else if (holder->defined_by_other_loaders()) {
278 Key k(holder);
279 if (CDS_ONLY(!Key::can_compute_cds_hash(&k)) NOT_CDS(true)) {
280 ++left;
281 }
282 }
283 }
284 }
285 return left;
286 }
287
288 void CompileTrainingData::print_on(outputStream* st, bool name_only) const {
289 _method->print_on(st, true);
290 st->print("#%dL%d", _compile_id, _level);
291 if (name_only) {
292 return;
293 }
294 if (_init_deps.length() > 0) {
295 if (_init_deps_left > 0) {
296 st->print(" udeps=%d", _init_deps_left);
297 }
298 for (int i = 0, len = _init_deps.length(); i < len; i++) {
299 st->print(" dep:");
300 _init_deps.at(i)->print_on(st, true);
301 }
302 }
303 }
304
305 void CompileTrainingData::notice_inlined_method(CompileTask* task,
306 const methodHandle& method) {
307 MethodTrainingData* mtd = MethodTrainingData::make(method);
308 if (mtd != nullptr) {
309 mtd->notice_compilation(task->comp_level(), true);
310 }
311 }
312
313 void CompileTrainingData::notice_jit_observation(ciEnv* env, ciBaseObject* what) {
314 // A JIT is starting to look at class k.
315 // We could follow the queries that it is making, but it is
316 // simpler to assume, conservatively, that the JIT will
317 // eventually depend on the initialization state of k.
318 ciMetadata* md = nullptr;
319 if (what->is_object()) {
320 md = what->as_object()->klass();
321 } else if (what->is_metadata()) {
322 md = what->as_metadata();
323 }
324 if (md != nullptr && md->is_loaded() && md->is_instance_klass()) {
325 ciInstanceKlass* cik = md->as_instance_klass();
326 if (!cik->is_initialized()) {
327 return;
328 }
329 KlassTrainingData* ktd = KlassTrainingData::make(cik->get_instanceKlass());
330 if (ktd == nullptr) {
331 // Allocation failure or snapshot in progress
332 return;
333 }
334 // This JIT task is (probably) requesting that ik be initialized,
335 // so add it to my _init_deps list.
336 TrainingDataLocker l;
337 if (l.can_add()) {
338 add_init_dep(ktd);
339 }
340 }
341 }
342
343 void KlassTrainingData::prepare(Visitor& visitor) {
344 if (visitor.is_visited(this)) {
345 return;
346 }
347 visitor.visit(this);
348 _comp_deps.prepare();
349 }
350
351 void MethodTrainingData::prepare(Visitor& visitor) {
352 if (visitor.is_visited(this)) {
353 return;
354 }
355 visitor.visit(this);
356 klass()->prepare(visitor);
357 if (has_holder()) {
358 _final_counters = holder()->method_counters();
359 _final_profile = holder()->method_data();
360 assert(_final_profile == nullptr || _final_profile->method() == holder(), "");
361 _invocation_count = holder()->invocation_count();
362 _backedge_count = holder()->backedge_count();
363 }
364 for (int i = 0; i < CompLevel_count - 1; i++) {
365 CompileTrainingData* ctd = _last_toplevel_compiles[i];
366 if (ctd != nullptr) {
367 ctd->prepare(visitor);
368 }
369 }
370 }
371
372 void CompileTrainingData::prepare(Visitor& visitor) {
373 if (visitor.is_visited(this)) {
374 return;
375 }
376 visitor.visit(this);
377 method()->prepare(visitor);
378 _init_deps.prepare();
379 _ci_records.prepare();
380 }
381
382 KlassTrainingData* KlassTrainingData::make(InstanceKlass* holder, bool null_if_not_found) {
383 Key key(holder);
384 TrainingData* td = CDS_ONLY((have_data() && !need_data()) ? lookup_archived_training_data(&key) :) nullptr;
385 KlassTrainingData* ktd = nullptr;
386 if (td != nullptr) {
387 ktd = td->as_KlassTrainingData();
388 guarantee(!ktd->has_holder() || ktd->holder() == holder, "");
389 if (ktd->has_holder()) {
390 return ktd;
391 } else {
392 ktd = nullptr;
393 }
394 }
395 if (need_data()) {
396 TrainingDataLocker l;
397 td = training_data_set()->find(&key);
398 if (td == nullptr) {
399 if (null_if_not_found) {
400 return nullptr;
401 }
402 ktd = KlassTrainingData::allocate(holder);
403 if (ktd == nullptr) {
404 return nullptr; // allocation failure
405 }
406 td = training_data_set()->install(ktd);
407 assert(ktd == td, "");
408 } else {
409 ktd = td->as_KlassTrainingData();
410 guarantee(ktd->holder() != nullptr, "null holder");
411 }
412 assert(ktd != nullptr, "");
413 guarantee(ktd->holder() == holder, "");
414 }
415 return ktd;
416 }
417
418 void KlassTrainingData::print_on(outputStream* st, bool name_only) const {
419 if (has_holder()) {
420 name()->print_symbol_on(st);
421 switch (holder()->init_state()) {
422 case InstanceKlass::allocated: st->print("[A]"); break;
423 case InstanceKlass::loaded: st->print("[D]"); break;
424 case InstanceKlass::linked: st->print("[L]"); break;
425 case InstanceKlass::being_initialized: st->print("[i]"); break;
426 case InstanceKlass::fully_initialized: break;
427 case InstanceKlass::initialization_error: st->print("[E]"); break;
428 default: fatal("unknown state: %d", holder()->init_state());
429 }
430 if (holder()->is_interface()) {
431 st->print("I");
432 }
433 } else {
434 st->print("[SYM]");
435 }
436 if (name_only) {
437 return;
438 }
439 if (_comp_deps.length() > 0) {
440 for (int i = 0, len = _comp_deps.length(); i < len; i++) {
441 st->print(" dep:");
442 _comp_deps.at(i)->print_on(st, true);
443 }
444 }
445 }
446
447 KlassTrainingData::KlassTrainingData(InstanceKlass* klass) : TrainingData(klass) {
448 assert(klass != nullptr, "");
449 // The OopHandle constructor will allocate a handle. We don't need to ever release it so we don't preserve
450 // the handle object.
451 OopHandle handle(Universe::vm_global(), klass->java_mirror());
452 _holder = klass;
453 assert(holder() == klass, "");
454 }
455
456 void KlassTrainingData::notice_fully_initialized() {
457 ResourceMark rm;
458 assert(has_holder(), "");
459 assert(holder()->is_initialized(), "wrong state: %s %s",
460 holder()->name()->as_C_string(), holder()->init_state_name());
461
462 TrainingDataLocker l; // Not a real lock if we don't collect the data,
463 // that's why we need the atomic decrement below.
464 for (int i = 0; i < comp_dep_count(); i++) {
465 comp_dep(i)->dec_init_deps_left_release(this);
466 }
467 holder()->set_has_init_deps_processed();
468 }
469
470 void TrainingData::init_dumptime_table(TRAPS) {
471 if (assembling_data() && !need_data()) {
472 _dumptime_training_data_dictionary = new DumptimeTrainingDataDictionary();
473 _archived_training_data_dictionary.iterate_all([&](TrainingData* record) {
474 _dumptime_training_data_dictionary->append(record);
475 });
476 }
477 if (need_data()) {
478 _dumptime_training_data_dictionary = new DumptimeTrainingDataDictionary();
479 TrainingDataLocker l;
480 TrainingDataLocker::snapshot();
481 ResourceMark rm;
482 Visitor visitor(training_data_set()->size());
483 training_data_set()->iterate([&](TrainingData* td) {
484 td->prepare(visitor);
485 if (!td->is_CompileTrainingData()) {
486 _dumptime_training_data_dictionary->append(td);
487 }
488 });
489 }
490
491 RecompilationSchedule::prepare(CHECK);
492
493 if (AOTVerifyTrainingData) {
494 TrainingData::verify();
495 }
496 }
497
498 void TrainingData::iterate_roots(MetaspaceClosure* it) {
499 if (_dumptime_training_data_dictionary != nullptr) {
500 for (int i = 0; i < _dumptime_training_data_dictionary->length(); i++) {
501 _dumptime_training_data_dictionary->at(i).metaspace_pointers_do(it);
502 }
503 }
504 RecompilationSchedule::iterate_roots(it);
505 }
506
507 void TrainingData::dump_training_data() {
508 if (_dumptime_training_data_dictionary != nullptr) {
509 CompactHashtableStats stats;
510 _archived_training_data_dictionary_for_dumping.reset();
511 CompactHashtableWriter writer(_dumptime_training_data_dictionary->length(), &stats);
512 for (int i = 0; i < _dumptime_training_data_dictionary->length(); i++) {
513 TrainingData* td = _dumptime_training_data_dictionary->at(i).training_data();
514 #ifdef ASSERT
515 for (int j = i+1; j < _dumptime_training_data_dictionary->length(); j++) {
516 TrainingData* td1 = _dumptime_training_data_dictionary->at(j).training_data();
517 assert(!TrainingData::Key::equals(td1, td->key(), -1), "conflict");
518 }
519 #endif // ASSERT
520 td = ArchiveBuilder::current()->get_buffered_addr(td);
521 uint hash = TrainingData::Key::cds_hash(td->key());
522 writer.add(hash, AOTCompressedPointers::encode_not_null(td));
523 }
524 writer.dump(&_archived_training_data_dictionary_for_dumping, "training data dictionary");
525 }
526 }
527
528 void TrainingData::cleanup_training_data() {
529 if (_dumptime_training_data_dictionary != nullptr) {
530 ResourceMark rm;
531 Visitor visitor(_dumptime_training_data_dictionary->length());
532 for (int i = 0; i < _dumptime_training_data_dictionary->length(); i++) {
533 TrainingData* td = _dumptime_training_data_dictionary->at(i).training_data();
534 td->cleanup(visitor);
535 }
536 // Throw away all elements with empty keys
537 int j = 0;
538 for (int i = 0; i < _dumptime_training_data_dictionary->length(); i++) {
539 TrainingData* td = _dumptime_training_data_dictionary->at(i).training_data();
540 if (td->key()->is_empty()) {
541 continue;
542 }
543 if (i != j) { // no need to copy if it's the same
544 _dumptime_training_data_dictionary->at_put(j, td);
545 }
546 j++;
547 }
548 _dumptime_training_data_dictionary->trunc_to(j);
549 }
550 RecompilationSchedule::cleanup();
551 }
552
553 void KlassTrainingData::cleanup(Visitor& visitor) {
554 if (visitor.is_visited(this)) {
555 return;
556 }
557 visitor.visit(this);
558 if (has_holder()) {
559 bool is_excluded = !holder()->is_loaded();
560 if (CDSConfig::is_at_aot_safepoint()) {
561 // Check for AOT exclusion only at AOT safe point.
562 is_excluded |= SystemDictionaryShared::should_be_excluded(holder());
563 }
564 if (is_excluded) {
565 ResourceMark rm;
566 log_debug(aot, training)("Cleanup KTD %s", name()->as_klass_external_name());
567 _holder = nullptr;
568 key()->make_empty();
569 }
570 }
571 for (int i = 0; i < _comp_deps.length(); i++) {
572 _comp_deps.at(i)->cleanup(visitor);
573 }
574 }
575
576 void MethodTrainingData::cleanup(Visitor& visitor) {
577 if (visitor.is_visited(this)) {
578 return;
579 }
580 visitor.visit(this);
581 if (has_holder()) {
582 if (CDSConfig::is_at_aot_safepoint() && SystemDictionaryShared::should_be_excluded(holder()->method_holder())) {
583 // Check for AOT exclusion only at AOT safe point.
584 log_debug(aot, training)("Cleanup MTD %s::%s", name()->as_klass_external_name(), signature()->as_utf8());
585 if (_final_profile != nullptr && _final_profile->method() != _holder) {
586 log_warning(aot, training)("Stale MDO for %s::%s", name()->as_klass_external_name(), signature()->as_utf8());
587 }
588 _final_profile = nullptr;
589 _final_counters = nullptr;
590 _holder = nullptr;
591 key()->make_empty();
592 }
593 }
594 for (int i = 0; i < CompLevel_count - 1; i++) {
595 CompileTrainingData* ctd = _last_toplevel_compiles[i];
596 if (ctd != nullptr) {
597 ctd->cleanup(visitor);
598 }
599 }
600 }
601
602 void KlassTrainingData::verify() {
603 for (int i = 0; i < comp_dep_count(); i++) {
604 CompileTrainingData* ctd = comp_dep(i);
605 if (!ctd->_init_deps.contains(this)) {
606 print_on(tty); tty->cr();
607 ctd->print_on(tty); tty->cr();
608 }
609 guarantee(ctd->_init_deps.contains(this), "");
610 }
611 }
612
613 void MethodTrainingData::verify(bool verify_dep_counter) {
614 iterate_compiles([&](CompileTrainingData* ctd) {
615 ctd->verify(verify_dep_counter);
616 });
617 }
618
619 void CompileTrainingData::verify(bool verify_dep_counter) {
620 for (int i = 0; i < init_dep_count(); i++) {
621 KlassTrainingData* ktd = init_dep(i);
622 if (ktd->has_holder() && ktd->holder()->defined_by_other_loaders()) {
623 LogStreamHandle(Info, training) log;
624 if (log.is_enabled()) {
625 ResourceMark rm;
626 log.print("CTD "); print_value_on(&log);
627 log.print(" depends on unregistered class %s", ktd->holder()->name()->as_C_string());
628 }
629 }
630 if (!ktd->_comp_deps.contains(this)) {
631 print_on(tty); tty->cr();
632 ktd->print_on(tty); tty->cr();
633 }
634 guarantee(ktd->_comp_deps.contains(this), "");
635 }
636
637 if (verify_dep_counter) {
638 int init_deps_left1 = init_deps_left_acquire();
639 int init_deps_left2 = compute_init_deps_left();
640
641 bool invariant = (init_deps_left1 >= init_deps_left2);
642 if (!invariant) {
643 print_on(tty);
644 tty->cr();
645 }
646 guarantee(invariant, "init deps invariant violation: %d >= %d", init_deps_left1, init_deps_left2);
647 }
648 }
649
650 void CompileTrainingData::cleanup(Visitor& visitor) {
651 if (visitor.is_visited(this)) {
652 return;
653 }
654 visitor.visit(this);
655 method()->cleanup(visitor);
656 }
657
658 void TrainingData::serialize(SerializeClosure* soc) {
659 if (soc->writing()) {
660 _archived_training_data_dictionary_for_dumping.serialize_header(soc);
661 } else {
662 _archived_training_data_dictionary.serialize_header(soc);
663 }
664 RecompilationSchedule::serialize(soc);
665 }
666
667 class TrainingDataPrinter : StackObj {
668 outputStream* _st;
669 int _index;
670 public:
671 TrainingDataPrinter(outputStream* st) : _st(st), _index(0) {}
672 void do_value(TrainingData* td) {
673 const char* type = (td->is_KlassTrainingData() ? "K" :
674 td->is_MethodTrainingData() ? "M" :
675 td->is_CompileTrainingData() ? "C" : "?");
676 _st->print("%4d: %p %s ", _index++, td, type);
677 td->print_on(_st);
678 _st->cr();
679 if (td->is_KlassTrainingData()) {
680 td->as_KlassTrainingData()->iterate_comp_deps([&](CompileTrainingData* ctd) {
681 ResourceMark rm;
682 _st->print_raw(" C ");
683 ctd->print_on(_st);
684 _st->cr();
685 });
686 } else if (td->is_MethodTrainingData()) {
687 td->as_MethodTrainingData()->iterate_compiles([&](CompileTrainingData* ctd) {
688 ResourceMark rm;
689 _st->print_raw(" C ");
690 ctd->print_on(_st);
691 _st->cr();
692 });
693 } else if (td->is_CompileTrainingData()) {
694 // ?
695 }
696 }
697 };
698
699 void TrainingData::print_archived_training_data_on(outputStream* st) {
700 st->print_cr("Archived TrainingData Dictionary");
701 TrainingDataPrinter tdp(st);
702 TrainingDataLocker::initialize();
703 _archived_training_data_dictionary.iterate_all(&tdp);
704 RecompilationSchedule::print_archived_training_data_on(st);
705 }
706
707 void TrainingData::Key::metaspace_pointers_do(MetaspaceClosure *iter) {
708 iter->push(const_cast<Metadata**>(&_meta));
709 }
710
711 void TrainingData::metaspace_pointers_do(MetaspaceClosure* iter) {
712 _key.metaspace_pointers_do(iter);
713 }
714
715 bool TrainingData::Key::can_compute_cds_hash(const Key* const& k) {
716 return k->meta() == nullptr || MetaspaceObj::in_aot_cache(k->meta());
717 }
718
719 uint TrainingData::Key::cds_hash(const Key* const& k) {
720 return SystemDictionaryShared::hash_for_shared_dictionary((address)k->meta());
721 }
722
723 TrainingData* TrainingData::lookup_archived_training_data(const Key* k) {
724 assert(!need_data(), "Should be used only in read-only mode");
725 // For this to work, all components of the key must be in shared metaspace.
726 if (!TrainingData::Key::can_compute_cds_hash(k) || _archived_training_data_dictionary.empty()) {
727 return nullptr;
728 }
729 uint hash = TrainingData::Key::cds_hash(k);
730 TrainingData* td = _archived_training_data_dictionary.lookup(k, hash, -1 /*unused*/);
731 if (td != nullptr) {
732 if ((td->is_KlassTrainingData() && td->as_KlassTrainingData()->has_holder()) ||
733 (td->is_MethodTrainingData() && td->as_MethodTrainingData()->has_holder())) {
734 return td;
735 } else {
736 ShouldNotReachHere();
737 }
738 }
739 return nullptr;
740 }
741
742 template <typename T>
743 void TrainingData::DepList<T>::metaspace_pointers_do(MetaspaceClosure* iter) {
744 iter->push(&_deps);
745 }
746
747 void KlassTrainingData::metaspace_pointers_do(MetaspaceClosure* iter) {
748 log_trace(aot, training)("Iter(KlassTrainingData): %p", this);
749 TrainingData::metaspace_pointers_do(iter);
750 _comp_deps.metaspace_pointers_do(iter);
751 iter->push(&_holder);
752 }
753
754 void MethodTrainingData::metaspace_pointers_do(MetaspaceClosure* iter) {
755 log_trace(aot, training)("Iter(MethodTrainingData): %p", this);
756 TrainingData::metaspace_pointers_do(iter);
757 iter->push(&_klass);
758 iter->push((Method**)&_holder);
759 for (int i = 0; i < CompLevel_count - 1; i++) {
760 iter->push(&_last_toplevel_compiles[i]);
761 }
762 iter->push(&_final_profile);
763 iter->push(&_final_counters);
764 }
765
766 void CompileTrainingData::metaspace_pointers_do(MetaspaceClosure* iter) {
767 log_trace(aot, training)("Iter(CompileTrainingData): %p", this);
768 TrainingData::metaspace_pointers_do(iter);
769 _init_deps.metaspace_pointers_do(iter);
770 _ci_records.metaspace_pointers_do(iter);
771 iter->push(&_method);
772 }
773
774 template <typename T>
775 void TrainingData::DepList<T>::prepare() {
776 if (_deps == nullptr && _deps_dyn != nullptr) {
777 int len = _deps_dyn->length();
778 _deps = MetadataFactory::new_array_from_c_heap<T>(len, mtClassShared);
779 for (int i = 0; i < len; i++) {
780 _deps->at_put(i, _deps_dyn->at(i)); // copy
781 }
782 _deps_dyn = nullptr;
783 }
784 }
785
786 void KlassTrainingData::remove_unshareable_info() {
787 TrainingData::remove_unshareable_info();
788 _comp_deps.remove_unshareable_info();
789 }
790
791 void MethodTrainingData::remove_unshareable_info() {
792 TrainingData::remove_unshareable_info();
793 if (_final_counters != nullptr) {
794 _final_counters->remove_unshareable_info();
795 }
796 if (_final_profile != nullptr) {
797 _final_profile->remove_unshareable_info();
798 }
799 }
800
801 void CompileTrainingData::remove_unshareable_info() {
802 TrainingData::remove_unshareable_info();
803 _init_deps.remove_unshareable_info();
804 _ci_records.remove_unshareable_info();
805 _init_deps_left = compute_init_deps_left(true);
806 }