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