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