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