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 #ifndef SHARE_OOPS_TRAININGDATA_HPP
 26 #define SHARE_OOPS_TRAININGDATA_HPP
 27 
 28 #include "cds/cdsConfig.hpp"
 29 #include "classfile/classLoaderData.hpp"
 30 #include "classfile/compactHashtable.hpp"
 31 #include "compiler/compilerDefinitions.hpp"
 32 #include "compiler/compiler_globals.hpp"
 33 #include "memory/allocation.hpp"
 34 #include "memory/metaspaceClosure.hpp"
 35 #include "oops/instanceKlass.hpp"
 36 #include "oops/method.hpp"
 37 #include "runtime/handles.hpp"
 38 #include "runtime/mutexLocker.hpp"
 39 #include "utilities/resizeableResourceHash.hpp"
 40 
 41 class ciEnv;
 42 class ciBaseObject;
 43 class CompileTask;
 44 class CompileTrainingData;
 45 class KlassTrainingData;
 46 class MethodTrainingData;
 47 
 48 // Base class for all the training data varieties
 49 class TrainingData : public Metadata {
 50   friend KlassTrainingData;
 51   friend MethodTrainingData;
 52   friend CompileTrainingData;
 53 public:
 54   // Key is used to insert any TrainingData (TD) object into a hash tables. The key is currently a
 55   // pointer to a metaspace object the TD is associated with. For example,
 56   // for KlassTrainingData it's an InstanceKlass, for MethodTrainingData it's a Method.
 57   // The utility of the these hash tables is to be able to find a TD object for a given metaspace
 58   // metaspace object.
 59   class Key {
 60     mutable Metadata* _meta;
 61     // These guys can get to my constructors:
 62     friend TrainingData;
 63     friend KlassTrainingData;
 64     friend MethodTrainingData;
 65     friend CompileTrainingData;
 66 
 67     // The empty key
 68     Key() : _meta(nullptr) { }
 69     bool is_empty() const { return _meta == nullptr; }
 70   public:
 71     Key(Metadata* meta) : _meta(meta) { }
 72 
 73     static bool can_compute_cds_hash(const Key* const& k);
 74     static uint cds_hash(const Key* const& k);
 75     static unsigned hash(const Key* const& k) {
 76       return primitive_hash(k->meta());
 77     }
 78     static bool equals(const Key* const& k1, const Key* const& k2) {
 79       return k1->meta() == k2->meta();
 80     }
 81     static inline bool equals(TrainingData* value, const TrainingData::Key* key, int unused) {
 82       return equals(value->key(), key);
 83     }
 84     int cmp(const Key* that) const {
 85       auto m1 = this->meta();
 86       auto m2 = that->meta();
 87       if (m1 < m2) return -1;
 88       if (m1 > m2) return +1;
 89       return 0;
 90     }
 91     Metadata* meta() const { return _meta; }
 92     void metaspace_pointers_do(MetaspaceClosure *iter);
 93     void make_empty() const { _meta = nullptr; }
 94   };
 95 
 96   // TrainingDataLocker is used to guard read/write operations on non-MT-safe data structures.
 97   // It supports recursive locking and a read-only mode (in which case no locks are taken).
 98   // It is also a part of the TD collection termination protocol (see the "spanshot" field).
 99   class TrainingDataLocker {
100     static volatile bool _snapshot; // If true we're not allocating new training data
101     static int _lock_mode;
102     const bool _recursive;
103     static void lock() {
104 #if INCLUDE_CDS
105       assert(_lock_mode != 0, "Forgot to call TrainingDataLocker::initialize()");
106       if (_lock_mode > 0) {
107         TrainingData_lock->lock();
108       }
109 #endif
110     }
111     static void unlock() {
112 #if INCLUDE_CDS
113       if (_lock_mode > 0) {
114         TrainingData_lock->unlock();
115       }
116 #endif
117     }
118     static bool safely_locked() {
119 #if INCLUDE_CDS
120       assert(_lock_mode != 0, "Forgot to call TrainingDataLocker::initialize()");
121       if (_lock_mode > 0) {
122         return is_self_locked();
123       } else {
124         return true;
125       }
126 #else
127       return true;
128 #endif
129     }
130     static bool is_self_locked() {
131       return CDS_ONLY(TrainingData_lock->owned_by_self()) NOT_CDS(false);
132     }
133 
134   public:
135     static void snapshot() {
136 #if INCLUDE_CDS
137       assert_locked();
138       _snapshot = true;
139 #endif
140     }
141     static bool can_add() {
142 #if INCLUDE_CDS
143       assert_locked();
144       return !_snapshot;
145 #else
146       return false;
147 #endif
148     }
149     static void initialize() {
150 #if INCLUDE_CDS
151       _lock_mode = need_data() ? +1 : -1;   // if -1, we go lock-free
152 #endif
153     }
154     static void assert_locked() {
155       assert(safely_locked(), "use under TrainingDataLocker");
156     }
157     static void assert_can_add() {
158       assert(can_add(), "Cannot add TrainingData objects");
159     }
160     TrainingDataLocker() : _recursive(is_self_locked()) {
161       if (!_recursive) {
162         lock();
163       }
164     }
165     ~TrainingDataLocker() {
166       if (!_recursive) {
167         unlock();
168       }
169     }
170   };
171 
172   // A set of TD objects that we collect during the training run.
173   class TrainingDataSet {
174     friend TrainingData;
175     ResizeableResourceHashtable<const Key*, TrainingData*,
176                                 AnyObj::C_HEAP, MemTag::mtCompiler,
177                                 &TrainingData::Key::hash,
178                                 &TrainingData::Key::equals>
179       _table;
180 
181   public:
182     template<typename... Arg>
183     TrainingDataSet(Arg... arg)
184       : _table(arg...) {
185     }
186     TrainingData* find(const Key* key) const {
187       TrainingDataLocker::assert_locked();
188       if (TrainingDataLocker::can_add()) {
189         auto res = _table.get(key);
190         return res == nullptr ? nullptr : *res;
191       }
192       return nullptr;
193     }
194     bool remove(const Key* key) {
195       return _table.remove(key);
196     }
197     TrainingData* install(TrainingData* td) {
198       TrainingDataLocker::assert_locked();
199       TrainingDataLocker::assert_can_add();
200       auto key = td->key();
201       if (key->is_empty()) {
202         return td;  // unkeyed TD not installed
203       }
204       bool created = false;
205       auto prior = _table.put_if_absent(key, td, &created);
206       if (prior == nullptr || *prior == td) {
207         return td;
208       }
209       assert(false, "no pre-existing elements allowed");
210       return *prior;
211     }
212     template<typename Function>
213     void iterate(const Function& fn) const { // lambda enabled API
214       iterate(const_cast<Function&>(fn));
215     }
216     template<typename Function>
217     void iterate(Function& fn) const { // lambda enabled API
218       return _table.iterate_all([&](const TrainingData::Key* k, TrainingData* td) { fn(td); });
219     }
220     int size() const { return _table.number_of_entries(); }
221 
222     void verify() const {
223       TrainingDataLocker::assert_locked();
224       iterate([&](TrainingData* td) { td->verify(); });
225     }
226   };
227 
228   // A widget to ensure that we visit TD object only once (TD objects can have pointer to
229   // other TD object that are sometimes circular).
230   class Visitor {
231     ResizeableResourceHashtable<TrainingData*, bool> _visited;
232   public:
233     Visitor(unsigned size) : _visited(size, 0x3fffffff) { }
234     bool is_visited(TrainingData* td) {
235       return _visited.contains(td);
236     }
237     void visit(TrainingData* td) {
238       bool created;
239       _visited.put_if_absent(td, &created);
240     }
241   };
242 
243   typedef OffsetCompactHashtable<const TrainingData::Key*, TrainingData*, TrainingData::Key::equals> TrainingDataDictionary;
244 private:
245   Key _key;
246 
247   // just forward all constructor arguments to the embedded key
248   template<typename... Arg>
249   TrainingData(Arg... arg)
250     : _key(arg...) { }
251 
252   // Container for recording TD during training run
253   static TrainingDataSet _training_data_set;
254   // Containter for replaying the training data (read-only, populated from the AOT image)
255   static TrainingDataDictionary _archived_training_data_dictionary;
256   // Container used for writing the AOT image
257   static TrainingDataDictionary _archived_training_data_dictionary_for_dumping;
258   class DumpTimeTrainingDataInfo {
259     TrainingData* _training_data;
260   public:
261     DumpTimeTrainingDataInfo() : DumpTimeTrainingDataInfo(nullptr) {}
262     DumpTimeTrainingDataInfo(TrainingData* training_data) : _training_data(training_data) {}
263     void metaspace_pointers_do(MetaspaceClosure* it) {
264       it->push(&_training_data);
265     }
266     TrainingData* training_data() {
267       return _training_data;
268     }
269   };
270   typedef GrowableArrayCHeap<DumpTimeTrainingDataInfo, mtClassShared> DumptimeTrainingDataDictionary;
271   // A temporary container that is used to accumulate and filter TD during dumping
272   static DumptimeTrainingDataDictionary* _dumptime_training_data_dictionary;
273 
274   static TrainingDataSet* training_data_set() { return &_training_data_set; }
275   static TrainingDataDictionary* archived_training_data_dictionary() { return &_archived_training_data_dictionary; }
276 
277  public:
278   // Returns the key under which this TD is installed, or else
279   // Key::EMPTY if it is not installed.
280   const Key* key() const { return &_key; }
281 
282   static bool have_data() { return AOTReplayTraining;  } // Going to read
283   static bool need_data() { return AOTRecordTraining;  } // Going to write
284   static bool assembling_data() { return have_data() && CDSConfig::is_dumping_final_static_archive() && CDSConfig::is_dumping_aot_linked_classes(); }
285 
286   template<typename Function>
287   static void iterate(const Function& fn) { iterate(const_cast<Function&>(fn)); }
288 
289   template<typename Function>
290   static void iterate(Function& fn) { // lambda enabled API
291     TrainingDataLocker l;
292     if (have_data()) {
293       archived_training_data_dictionary()->iterate(fn);
294     }
295     if (need_data()) {
296       training_data_set()->iterate(fn);
297     }
298   }
299 
300   virtual MethodTrainingData*   as_MethodTrainingData()  const { return nullptr; }
301   virtual KlassTrainingData*    as_KlassTrainingData()   const { return nullptr; }
302   virtual CompileTrainingData*  as_CompileTrainingData() const { return nullptr; }
303   bool is_MethodTrainingData()  const { return as_MethodTrainingData()  != nullptr; }
304   bool is_KlassTrainingData()   const { return as_KlassTrainingData()   != nullptr; }
305   bool is_CompileTrainingData() const { return as_CompileTrainingData() != nullptr; }
306 
307   virtual void prepare(Visitor& visitor) = 0;
308   virtual void cleanup(Visitor& visitor) = 0;
309 
310   static void initialize() NOT_CDS_RETURN;
311 
312   static void verify();
313 
314   // Widget for recording dependencies, as an N-to-M graph relation,
315   // possibly cyclic.
316   template<typename E>
317   class DepList : public StackObj {
318     GrowableArrayCHeap<E, mtCompiler>* _deps_dyn;
319     Array<E>*                          _deps;
320   public:
321     DepList() {
322       _deps_dyn = nullptr;
323       _deps = nullptr;
324     }
325 
326     int length() const {
327       return (_deps_dyn != nullptr ? _deps_dyn->length()
328               : _deps   != nullptr ? _deps->length()
329               : 0);
330     }
331     E* adr_at(int i) const {
332       return (_deps_dyn != nullptr ? _deps_dyn->adr_at(i)
333               : _deps   != nullptr ? _deps->adr_at(i)
334               : nullptr);
335     }
336     E at(int i) const {
337       assert(i >= 0 && i < length(), "oob");
338       return *adr_at(i);
339     }
340     bool append_if_missing(E dep) {
341       if (_deps_dyn == nullptr) {
342         _deps_dyn = new GrowableArrayCHeap<E, mtCompiler>(10);
343         _deps_dyn->append(dep);
344         return true;
345       } else {
346         return _deps_dyn->append_if_missing(dep);
347       }
348     }
349     bool remove_if_existing(E dep) {
350       if (_deps_dyn != nullptr) {
351         return _deps_dyn->remove_if_existing(dep);
352       }
353       return false;
354     }
355     void clear() {
356       if (_deps_dyn != nullptr)  {
357         _deps_dyn->clear();
358       }
359     }
360     void append(E dep) {
361       if (_deps_dyn == nullptr) {
362         _deps_dyn = new GrowableArrayCHeap<E, mtCompiler>(10);
363       }
364       _deps_dyn->append(dep);
365     }
366     bool contains(E dep) {
367       for (int i = 0; i < length(); i++) {
368         if (dep == at(i)) {
369           return true; // found
370         }
371       }
372       return false; // not found
373     }
374 
375 #if INCLUDE_CDS
376     void remove_unshareable_info() {
377       _deps_dyn = nullptr;
378     }
379 #endif
380     void prepare(ClassLoaderData* loader_data);
381     void metaspace_pointers_do(MetaspaceClosure *iter);
382   };
383 
384   virtual void metaspace_pointers_do(MetaspaceClosure *iter);
385 
386   static void init_dumptime_table(TRAPS);
387 
388 #if INCLUDE_CDS
389   virtual void remove_unshareable_info() {}
390   static void iterate_roots(MetaspaceClosure* it);
391   static void dump_training_data();
392   static void cleanup_training_data();
393   static void serialize(SerializeClosure* soc);
394   static void print_archived_training_data_on(outputStream* st);
395   static TrainingData* lookup_archived_training_data(const Key* k);
396 #endif
397 
398   template<typename TrainingDataType, typename... ArgTypes>
399   static TrainingDataType* allocate(ArgTypes... args) {
400     assert(need_data() || have_data(), "");
401     if (TrainingDataLocker::can_add()) {
402       return new (mtClassShared) TrainingDataType(args...);
403     }
404     return nullptr;
405   }
406 };
407 
408 // Training data that is associated with an InstanceKlass
409 class KlassTrainingData : public TrainingData {
410   friend TrainingData;
411   friend CompileTrainingData;
412 
413   // Used by CDS. These classes need to access the private default constructor.
414   template <class T> friend class CppVtableTesterA;
415   template <class T> friend class CppVtableTesterB;
416   template <class T> friend class CppVtableCloner;
417 
418   // cross-link to live klass, or null if not loaded or encountered yet
419   InstanceKlass* _holder;
420   jobject _holder_mirror;   // extra link to prevent unloading by GC
421 
422   DepList<CompileTrainingData*> _comp_deps; // compiles that depend on me
423 
424   KlassTrainingData();
425   KlassTrainingData(InstanceKlass* klass);
426 
427   int comp_dep_count() const {
428     TrainingDataLocker::assert_locked();
429     return _comp_deps.length();
430   }
431   CompileTrainingData* comp_dep(int i) const {
432     TrainingDataLocker::assert_locked();
433     return _comp_deps.at(i);
434   }
435   void add_comp_dep(CompileTrainingData* ctd) {
436     TrainingDataLocker::assert_locked();
437      _comp_deps.append_if_missing(ctd);
438   }
439   void remove_comp_dep(CompileTrainingData* ctd) {
440     TrainingDataLocker::assert_locked();
441      _comp_deps.remove_if_existing(ctd);
442   }
443 
444  public:
445   Symbol* name() const {
446     precond(has_holder());
447     return holder()->name();
448   }
449   bool has_holder()       const { return _holder != nullptr; }
450   InstanceKlass* holder() const { return _holder; }
451 
452   static KlassTrainingData* make(InstanceKlass* holder,
453                                  bool null_if_not_found = false) NOT_CDS_RETURN_(nullptr);
454   static KlassTrainingData* find(InstanceKlass* holder) {
455     return make(holder, true);
456   }
457   virtual KlassTrainingData* as_KlassTrainingData() const { return const_cast<KlassTrainingData*>(this); };
458 
459   ClassLoaderData* class_loader_data() {
460     assert(has_holder(), "");
461     return holder()->class_loader_data();
462   }
463   void notice_fully_initialized() NOT_CDS_RETURN;
464 
465   void print_on(outputStream* st, bool name_only) const;
466   virtual void print_on(outputStream* st) const { print_on(st, false); }
467   virtual void print_value_on(outputStream* st) const { print_on(st, true); }
468 
469   virtual void prepare(Visitor& visitor);
470   virtual void cleanup(Visitor& visitor) NOT_CDS_RETURN;
471 
472   MetaspaceObj::Type type() const {
473     return KlassTrainingDataType;
474   }
475 
476 #if INCLUDE_CDS
477   virtual void remove_unshareable_info();
478 #endif
479 
480   void metaspace_pointers_do(MetaspaceClosure *iter);
481 
482   int size() const {
483     return (int)align_metadata_size(align_up(sizeof(KlassTrainingData), BytesPerWord)/BytesPerWord);
484   }
485 
486   const char* internal_name() const {
487     return "{ klass training data }";
488   };
489 
490   void verify();
491 
492   static KlassTrainingData* allocate(InstanceKlass* holder) {
493     return TrainingData::allocate<KlassTrainingData>(holder);
494   }
495 
496   template<typename Function>
497   void iterate_comp_deps(Function fn) const { // lambda enabled API
498     TrainingDataLocker l;
499     for (int i = 0; i < comp_dep_count(); i++) {
500       fn(comp_dep(i));
501     }
502   }
503 };
504 
505 // Information about particular JIT tasks.
506 class CompileTrainingData : public TrainingData {
507   friend TrainingData;
508   friend KlassTrainingData;
509 
510   // Used by CDS. These classes need to access the private default constructor.
511   template <class T> friend class CppVtableTesterA;
512   template <class T> friend class CppVtableTesterB;
513   template <class T> friend class CppVtableCloner;
514 
515   MethodTrainingData* _method;
516   const short _level;
517   const int _compile_id;
518 
519   // classes that should be initialized before this JIT task runs
520   DepList<KlassTrainingData*> _init_deps;
521   // Number of uninitialized classes left, when it's 0, all deps are satisfied
522   volatile int _init_deps_left;
523 
524 public:
525   // ciRecords is a generic meachanism to memoize CI responses to arbitary queries. For each function we're interested in we record
526   // (return_value, argument_values) tuples in a list. Arguments are allowed to have Metaspace pointers in them.
527   class ciRecords {
528     template <typename... Ts> class Arguments {
529     public:
530       bool operator==(const Arguments<>&) const { return true; }
531       void metaspace_pointers_do(MetaspaceClosure *iter) { }
532     };
533     template <typename T, typename... Ts> class Arguments<T, Ts...> {
534     private:
535       T _first;
536       Arguments<Ts...> _remaining;
537 
538     public:
539       constexpr Arguments(const T& first, const Ts&... remaining) noexcept
540         : _first(first), _remaining(remaining...) {}
541       constexpr Arguments() noexcept : _first(), _remaining() {}
542       bool operator==(const Arguments<T, Ts...>& that) const {
543         return _first == that._first && _remaining == that._remaining;
544       }
545       template<typename U = T, ENABLE_IF(std::is_pointer<U>::value && std::is_base_of<MetaspaceObj, typename std::remove_pointer<U>::type>::value)>
546       void metaspace_pointers_do(MetaspaceClosure *iter) {
547         iter->push(&_first);
548         _remaining.metaspace_pointers_do(iter);
549       }
550       template<typename U = T, ENABLE_IF(!(std::is_pointer<U>::value && std::is_base_of<MetaspaceObj, typename std::remove_pointer<U>::type>::value))>
551       void metaspace_pointers_do(MetaspaceClosure *iter) {
552         _remaining.metaspace_pointers_do(iter);
553       }
554     };
555 
556     template <typename ReturnType, typename... Args> class ciMemoizedFunction : public StackObj {
557     public:
558       class OptionalReturnType {
559         bool _valid;
560         ReturnType _result;
561       public:
562         OptionalReturnType(bool valid, const ReturnType& result) : _valid(valid), _result(result) {}
563         bool is_valid() const { return _valid; }
564         ReturnType result() const { return _result; }
565       };
566     private:
567       typedef Arguments<Args...> ArgumentsType;
568       class Record : public MetaspaceObj {
569         ReturnType    _result;
570         ArgumentsType _arguments;
571       public:
572         Record(const ReturnType& result, const ArgumentsType& arguments) : _result(result), _arguments(arguments) {}
573         Record() { }
574         ReturnType result() const { return _result; }
575         ArgumentsType arguments() const { return _arguments; }
576         bool operator==(const Record& that) { return _arguments == that._arguments; }
577         void metaspace_pointers_do(MetaspaceClosure *iter) { _arguments.metaspace_pointers_do(iter); }
578       };
579       DepList<Record> _data;
580     public:
581       OptionalReturnType find(const Args&... args) {
582         ArgumentsType a(args...);
583         for (int i = 0; i < _data.length(); i++) {
584           if (_data.at(i).arguments() == a) {
585             return OptionalReturnType(true, _data.at(i).result());
586           }
587         }
588         return OptionalReturnType(false, ReturnType());
589       }
590       bool append_if_missing(const ReturnType& result, const Args&... args) {
591         return _data.append_if_missing(Record(result, ArgumentsType(args...)));
592       }
593 #if INCLUDE_CDS
594       void remove_unshareable_info() { _data.remove_unshareable_info(); }
595 #endif
596       void prepare(ClassLoaderData* loader_data) {
597         _data.prepare(loader_data);
598       }
599       void metaspace_pointers_do(MetaspaceClosure *iter) {
600         _data.metaspace_pointers_do(iter);
601       }
602     };
603 
604 
605 public:
606     // Record CI answers for the InlineSmallCode heuristic. It is importance since the heuristic is non-commutative and we may want to
607     // compile methods in a different order than in the training run.
608     typedef ciMemoizedFunction<int, MethodTrainingData*> ciMethod__inline_instructions_size_type;
609     ciMethod__inline_instructions_size_type ciMethod__inline_instructions_size;
610 #if INCLUDE_CDS
611     void remove_unshareable_info() {
612       ciMethod__inline_instructions_size.remove_unshareable_info();
613     }
614 #endif
615     void prepare(ClassLoaderData* loader_data) {
616       ciMethod__inline_instructions_size.prepare(loader_data);
617     }
618     void metaspace_pointers_do(MetaspaceClosure *iter) {
619       ciMethod__inline_instructions_size.metaspace_pointers_do(iter);
620     }
621   };
622 
623 private:
624   ciRecords _ci_records;
625 
626   CompileTrainingData();
627   CompileTrainingData(MethodTrainingData* mtd,
628                       int level,
629                       int compile_id)
630       : TrainingData(),  // empty key
631         _method(mtd), _level(level), _compile_id(compile_id), _init_deps_left(0) { }
632 public:
633   ciRecords& ci_records() { return _ci_records; }
634   static CompileTrainingData* make(CompileTask* task) NOT_CDS_RETURN_(nullptr);
635 
636   virtual CompileTrainingData* as_CompileTrainingData() const { return const_cast<CompileTrainingData*>(this); };
637 
638   MethodTrainingData* method() const { return _method; }
639 
640   int level() const { return _level; }
641 
642   int compile_id() const { return _compile_id; }
643 
644   int init_dep_count() const {
645     TrainingDataLocker::assert_locked();
646     return _init_deps.length();
647   }
648   KlassTrainingData* init_dep(int i) const {
649     TrainingDataLocker::assert_locked();
650     return _init_deps.at(i);
651   }
652   void add_init_dep(KlassTrainingData* ktd) {
653     TrainingDataLocker::assert_locked();
654     ktd->add_comp_dep(this);
655     _init_deps.append_if_missing(ktd);
656   }
657   void clear_init_deps() {
658     TrainingDataLocker::assert_locked();
659     for (int i = 0; i < _init_deps.length(); i++) {
660       _init_deps.at(i)->remove_comp_dep(this);
661     }
662     _init_deps.clear();
663   }
664   void dec_init_deps_left(KlassTrainingData* ktd);
665   int init_deps_left() const {
666     return Atomic::load(&_init_deps_left);
667   }
668   uint compute_init_deps_left(bool count_initialized = false);
669 
670   void notice_inlined_method(CompileTask* task, const methodHandle& method) NOT_CDS_RETURN;
671 
672   // The JIT looks at classes and objects too and can depend on their state.
673   // These simple calls just report the *possibility* of an observation.
674   void notice_jit_observation(ciEnv* env, ciBaseObject* what) NOT_CDS_RETURN;
675 
676   virtual void prepare(Visitor& visitor);
677   virtual void cleanup(Visitor& visitor) NOT_CDS_RETURN;
678 
679   void print_on(outputStream* st, bool name_only) const;
680   virtual void print_on(outputStream* st) const { print_on(st, false); }
681   virtual void print_value_on(outputStream* st) const { print_on(st, true); }
682 
683 #if INCLUDE_CDS
684   virtual void remove_unshareable_info();
685 #endif
686 
687   virtual void metaspace_pointers_do(MetaspaceClosure* iter);
688   virtual MetaspaceObj::Type type() const { return CompileTrainingDataType; }
689 
690   virtual const char* internal_name() const {
691     return "{ compile training data }";
692   };
693 
694   virtual int size() const {
695     return (int)align_metadata_size(align_up(sizeof(CompileTrainingData), BytesPerWord)/BytesPerWord);
696   }
697 
698   void verify();
699 
700   static CompileTrainingData* allocate(MethodTrainingData* mtd, int level, int compile_id) {
701     return TrainingData::allocate<CompileTrainingData>(mtd, level, compile_id);
702   }
703 };
704 
705 // Record information about a method at the time compilation is requested.
706 class MethodTrainingData : public TrainingData {
707   friend TrainingData;
708   friend CompileTrainingData;
709 
710   // Used by CDS. These classes need to access the private default constructor.
711   template <class T> friend class CppVtableTesterA;
712   template <class T> friend class CppVtableTesterB;
713   template <class T> friend class CppVtableCloner;
714 
715   KlassTrainingData* _klass;
716   Method* _holder;
717   CompileTrainingData* _last_toplevel_compiles[CompLevel_count - 1];
718   int _highest_top_level;
719   int _level_mask;  // bit-set of all possible levels
720   bool _was_toplevel;
721   // metadata snapshots of final state:
722   MethodCounters* _final_counters;
723   MethodData*     _final_profile;
724 
725   MethodTrainingData();
726   MethodTrainingData(Method* method, KlassTrainingData* ktd) : TrainingData(method) {
727     _klass = ktd;
728     _holder = method;
729     for (int i = 0; i < CompLevel_count - 1; i++) {
730       _last_toplevel_compiles[i] = nullptr;
731     }
732     _highest_top_level = CompLevel_none;
733     _level_mask = 0;
734     _was_toplevel = false;
735   }
736 
737   static int level_mask(int level) {
738     return ((level & 0xF) != level ? 0 : 1 << level);
739   }
740 
741  public:
742   KlassTrainingData* klass()  const { return _klass; }
743   bool has_holder()           const { return _holder != nullptr; }
744   Method* holder()            const { return _holder; }
745   bool only_inlined()         const { return !_was_toplevel; }
746   bool saw_level(CompLevel l) const { return (_level_mask & level_mask(l)) != 0; }
747   int highest_top_level()     const { return _highest_top_level; }
748   MethodData* final_profile() const { return _final_profile; }
749 
750   Symbol* name() const {
751     precond(has_holder());
752     return holder()->name();
753   }
754   Symbol* signature() const {
755     precond(has_holder());
756     return holder()->signature();
757   }
758 
759   CompileTrainingData* last_toplevel_compile(int level) const {
760     if (level > CompLevel_none) {
761       return _last_toplevel_compiles[level - 1];
762     }
763     return nullptr;
764   }
765 
766   void notice_compilation(int level, bool inlined = false) {
767     if (!inlined) {
768       _was_toplevel = true;
769     }
770     _level_mask |= level_mask(level);
771   }
772 
773   void notice_toplevel_compilation(int level) {
774     _highest_top_level = MAX2(_highest_top_level, level);
775   }
776 
777   static MethodTrainingData* make(const methodHandle& method,
778                                   bool null_if_not_found = false,
779                                   bool use_cache = true) NOT_CDS_RETURN_(nullptr);
780   static MethodTrainingData* find_fast(const methodHandle& method) { return make(method, true, true); }
781   static MethodTrainingData* find(const methodHandle& method) { return make(method, true, false); }
782 
783   virtual MethodTrainingData* as_MethodTrainingData() const {
784     return const_cast<MethodTrainingData*>(this);
785   };
786 
787   void print_on(outputStream* st, bool name_only) const;
788   virtual void print_on(outputStream* st) const { print_on(st, false); }
789   virtual void print_value_on(outputStream* st) const { print_on(st, true); }
790 
791   virtual void prepare(Visitor& visitor);
792   virtual void cleanup(Visitor& visitor) NOT_CDS_RETURN;
793 
794   template<typename Function>
795   void iterate_compiles(Function fn) const { // lambda enabled API
796     for (int i = 0; i < CompLevel_count - 1; i++) {
797       CompileTrainingData* ctd = _last_toplevel_compiles[i];
798       if (ctd != nullptr) {
799         fn(ctd);
800       }
801     }
802   }
803 
804   virtual void metaspace_pointers_do(MetaspaceClosure* iter);
805   virtual MetaspaceObj::Type type() const { return MethodTrainingDataType; }
806 
807 #if INCLUDE_CDS
808   virtual void remove_unshareable_info();
809 #endif
810 
811   virtual int size() const {
812     return (int)align_metadata_size(align_up(sizeof(MethodTrainingData), BytesPerWord)/BytesPerWord);
813   }
814 
815   virtual const char* internal_name() const {
816     return "{ method training data }";
817   };
818 
819   void verify();
820 
821   static MethodTrainingData* allocate(Method* m, KlassTrainingData* ktd) {
822     return TrainingData::allocate<MethodTrainingData>(m, ktd);
823   }
824 };
825 #endif // SHARE_OOPS_TRAININGDATA_HPP