1 /*
  2  * Copyright (c) 2023, 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 SHARED_CDS_METHODDATAINFO_HPP
 26 #define SHARED_CDS_METHODDATAINFO_HPP
 27 
 28 #include "cds/archiveUtils.hpp"
 29 #include "cds/metaspaceShared.hpp"
 30 #include "classfile/compactHashtable.hpp"
 31 #include "classfile/javaClasses.hpp"
 32 #include "memory/metaspaceClosure.hpp"
 33 #include "oops/methodCounters.hpp"
 34 #include "oops/methodData.hpp"
 35 #include "utilities/growableArray.hpp"
 36 #include "utilities/resourceHash.hpp"
 37 
 38 class InstanceKlass;
 39 class Method;
 40 class Symbol;
 41 
 42 class MethodDataKey {
 43   Method* _holder;
 44 public:
 45   MethodDataKey(Method* holder) : _holder(holder) {}
 46 
 47   void metaspace_pointers_do(MetaspaceClosure* it) {
 48     it->push(&_holder);
 49   }
 50 
 51   bool equals(MethodDataKey const& other) const {
 52     return _holder == other._holder;
 53   }
 54 
 55   void mark_pointers();
 56   unsigned int hash() const;
 57 
 58   static unsigned int dumptime_hash(Symbol* sym)  {
 59     if (sym == nullptr) {
 60       // _invoked_name maybe null
 61       return 0;
 62     }
 63     return java_lang_String::hash_code((const jbyte*)sym->bytes(), sym->utf8_length());
 64   }
 65 
 66   unsigned int dumptime_hash() const {
 67     return dumptime_hash(_holder->name()) +
 68            dumptime_hash(_holder->signature());
 69   }
 70 
 71   static inline unsigned int DUMPTIME_HASH(MethodDataKey const& key) {
 72     return (key.dumptime_hash());
 73   }
 74 
 75   static inline bool DUMPTIME_EQUALS(
 76       MethodDataKey const& k1, MethodDataKey const& k2) {
 77     return (k1.equals(k2));
 78   }
 79 
 80   void init_for_archive(MethodDataKey& dumptime_key);
 81   Method* method() const { return _holder; }
 82 };
 83 
 84 class DumpTimeMethodDataInfo {
 85   MethodData*     _method_data;
 86   MethodCounters* _method_counters;
 87 public:
 88   DumpTimeMethodDataInfo(MethodData* method_data, MethodCounters* counters)
 89   : _method_data(method_data), _method_counters(counters) {}
 90 
 91   void metaspace_pointers_do(MetaspaceClosure* it) {
 92     it->push(&_method_data);
 93     it->push(&_method_counters);
 94   }
 95 
 96   MethodData* method_data() {
 97      return _method_data;
 98   }
 99   MethodCounters* method_counters() {
100     return _method_counters;
101   }
102 };
103 
104 class RunTimeMethodDataInfo {
105   MethodDataKey _key;
106   MethodData* _method_data;
107   MethodCounters* _method_counters;
108 public:
109   RunTimeMethodDataInfo(MethodDataKey key, MethodData* method_data, MethodCounters* counters) :
110       _key(key), _method_data(method_data), _method_counters(counters) {}
111 
112   // Used by MethodDataDictionary to implement OffsetCompactHashtable::EQUALS
113   static inline bool EQUALS(
114       const RunTimeMethodDataInfo* value, MethodDataKey* key, int len_unused) {
115     return (value->_key.equals(*key));
116   }
117   void init(MethodDataKey& key, DumpTimeMethodDataInfo& info) {
118     _key.init_for_archive(key);
119     ArchiveBuilder::current()->write_pointer_in_buffer(&_method_data, info.method_data());
120     ArchiveBuilder::current()->write_pointer_in_buffer(&_method_counters, info.method_counters());
121   }
122 
123   unsigned int hash() const {
124     return _key.hash();
125   }
126   MethodDataKey key() const {
127     return _key;
128   }
129 
130   Method*         method()          const { return _key.method();   }
131   MethodData*     method_data()     const { return _method_data;     }
132   MethodCounters* method_counters() const { return _method_counters; }
133 };
134 
135 class DumpTimeMethodInfoDictionary
136     : public ResourceHashtable<MethodDataKey,
137         DumpTimeMethodDataInfo,
138         137, // prime number
139         AnyObj::C_HEAP,
140         mtClassShared,
141         MethodDataKey::DUMPTIME_HASH,
142         MethodDataKey::DUMPTIME_EQUALS> {
143 public:
144   DumpTimeMethodInfoDictionary() : _count(0) {}
145   int _count;
146 };
147 
148 class MethodDataInfoDictionary : public OffsetCompactHashtable<
149     MethodDataKey*,
150     const RunTimeMethodDataInfo*,
151     RunTimeMethodDataInfo::EQUALS> {};
152 
153 #endif // SHARED_CDS_METHODDATAINFO_HPP