1 /*
  2  * Copyright (c) 2020, 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_VM_PRIMS_TAGMAPTABLE_HPP
 26 #define SHARE_VM_PRIMS_TAGMAPTABLE_HPP
 27 
 28 #include "gc/shared/collectedHeap.hpp"
 29 #include "memory/allocation.hpp"
 30 #include "oops/layoutKind.hpp"
 31 #include "oops/weakHandle.hpp"
 32 #include "utilities/resizableHashTable.hpp"
 33 
 34 class JvmtiEnv;
 35 
 36 // Describes an object which can be tagged during heap walk operation.
 37 // - generic heap object: _obj: oop, offset == 0, _inline_klass == nullptr;
 38 // - value heap object: _obj: oop, offset == 0, _inline_klass == _obj.klass();
 39 // - flat value object: _obj: holder object, offset == offset in the holder, _inline_klass == klass of the flattened object;
 40 class JvmtiHeapwalkObject {
 41   oop _obj;                   // for flattened value object this is holder object
 42   int _offset;                // == 0 for heap objects
 43   InlineKlass* _inline_klass; // for value object, nullptr otherwise
 44   LayoutKind _layout_kind;    // layout kind in holder object, used only for flat->heap conversion
 45 
 46   static InlineKlass* inline_klass_or_null(oop obj) {
 47     Klass* k = obj->klass();
 48     return k->is_inline_klass() ? InlineKlass::cast(k) : nullptr;
 49   }
 50 public:
 51   JvmtiHeapwalkObject(): _obj(nullptr), _offset(0), _inline_klass(nullptr), _layout_kind(LayoutKind::UNKNOWN) {}
 52   JvmtiHeapwalkObject(oop obj): _obj(obj), _offset(0), _inline_klass(inline_klass_or_null(obj)), _layout_kind(LayoutKind::REFERENCE) {}
 53   JvmtiHeapwalkObject(oop obj, int offset, InlineKlass* ik, LayoutKind lk): _obj(obj), _offset(offset), _inline_klass(ik), _layout_kind(lk) {}
 54 
 55   inline bool is_empty() const { return _obj == nullptr; }
 56   inline bool is_value() const { return _inline_klass != nullptr; }
 57   inline bool is_flat() const { return _offset != 0; }
 58 
 59   inline oop obj() const { return _obj; }
 60   inline int offset() const { return _offset; }
 61   inline InlineKlass* inline_klass() const { return _inline_klass; }
 62   inline LayoutKind layout_kind() const { return _layout_kind; }
 63 
 64   inline Klass* klass() const { return is_value() ? _inline_klass : obj()->klass(); }
 65 
 66   static bool equals(const JvmtiHeapwalkObject& obj1, const JvmtiHeapwalkObject& obj2);
 67 
 68   bool operator==(const JvmtiHeapwalkObject& other) const {
 69     // need to compare inline_klass too to handle the case when flat object has flat field at offset 0
 70     return _obj == other._obj && _offset == other._offset && _inline_klass == other._inline_klass;
 71   }
 72   bool operator!=(const JvmtiHeapwalkObject& other) const {
 73     return !(*this == other);
 74   }
 75 };
 76 
 77 // The oop is needed for lookup rather than creating a WeakHandle during
 78 // lookup because the HeapWalker may walk soon to be dead objects and
 79 // creating a WeakHandle for an otherwise dead object makes G1 unhappy.
 80 //
 81 // This class is the Key type for inserting in ResizeableHashTable
 82 // Its get_hash() and equals() methods are also used for getting the hash
 83 // value of a Key and comparing two Keys, respectively.
 84 //
 85 // Value objects: keep just one tag for all equal value objects including heap allocated value objects.
 86 // We have to keep a strong reference to each unique value object with a non-zero tag.
 87 // During heap walking flattened value object tags stored in separate JvmtiFlatTagMapTable,
 88 // converted to standard strong entries in JvmtiTagMapTable outside of sefepoint.
 89 // All equal value objects should have the same tag.
 90 // Keep value objects alive (1 copy for each "value") until their tags are removed.
 91 
 92 class JvmtiTagMapKey : public CHeapObj<mtServiceability> {
 93   union {
 94     WeakHandle _wh;
 95     OopHandle _h; // for value objects (_is_weak == false)
 96   };
 97   bool _is_weak;
 98   // temporarily hold obj while searching
 99   const JvmtiHeapwalkObject* _obj;
100 
101  public:
102   JvmtiTagMapKey(const JvmtiHeapwalkObject* obj);
103   // Copy ctor is called when we put entry to the hash table.
104   JvmtiTagMapKey(const JvmtiTagMapKey& src);
105   JvmtiTagMapKey& operator=(const JvmtiTagMapKey&) = delete;
106 
107   JvmtiHeapwalkObject heapwalk_object() const;
108 
109   oop object() const;
110   oop object_no_keepalive() const;
111   void release_handle();
112 
113   static unsigned get_hash(const JvmtiTagMapKey& entry);
114   static bool equals(const JvmtiTagMapKey& lhs, const JvmtiTagMapKey& rhs);
115 };
116 
117 typedef
118 ResizeableHashTable <JvmtiTagMapKey, jlong,
119                               AnyObj::C_HEAP, mtServiceability,
120                               JvmtiTagMapKey::get_hash,
121                               JvmtiTagMapKey::equals> ResizableHT;
122 
123 // A supporting class for iterating over all entries in Hashmap
124 class JvmtiTagMapKeyClosure {
125 public:
126   virtual bool do_entry(JvmtiTagMapKey& key, jlong& value) = 0;
127 };
128 
129 class JvmtiTagMapTable : public CHeapObj<mtServiceability> {
130  private:
131   ResizableHT _table;
132 
133   jlong* lookup(const JvmtiHeapwalkObject& obj) const;
134 
135  public:
136   JvmtiTagMapTable();
137   ~JvmtiTagMapTable();
138 
139   int number_of_entries() const { return _table.number_of_entries(); }
140 
141   jlong find(const JvmtiHeapwalkObject& obj) const;
142   // obj cannot be flat
143   void add(const JvmtiHeapwalkObject& obj, jlong tag);
144   // update the tag if the entry exists, returns false otherwise
145   bool update(const JvmtiHeapwalkObject& obj, jlong tag);
146 
147   bool remove(const JvmtiHeapwalkObject& obj);
148 
149   // iterate over all entries in the hashmap
150   void entry_iterate(JvmtiTagMapKeyClosure* closure);
151 
152   bool is_empty() const { return _table.number_of_entries() == 0; }
153 
154   // Cleanup cleared entries and store dead object tags in objects array
155   void remove_dead_entries(GrowableArray<jlong>* objects);
156   void clear();
157 };
158 
159 
160 // This class is the Key type for hash table to keep flattened value objects during heap walk operations.
161 // The objects needs to be moved to JvmtiTagMapTable outside of safepoint.
162 class JvmtiFlatTagMapKey: public CHeapObj<mtServiceability> {
163 private:
164   // holder object
165   OopHandle _h;
166   // temporarily holds holder object while searching
167   oop _holder;
168   int _offset;
169   InlineKlass* _inline_klass;
170   LayoutKind _layout_kind;
171 public:
172   JvmtiFlatTagMapKey(const JvmtiHeapwalkObject& obj);
173   // Copy ctor is called when we put entry to the hash table.
174   JvmtiFlatTagMapKey(const JvmtiFlatTagMapKey& src);
175 
176   JvmtiFlatTagMapKey& operator=(const JvmtiFlatTagMapKey&) = delete;
177 
178   JvmtiHeapwalkObject heapwalk_object() const;
179 
180   oop holder() const;
181   oop holder_no_keepalive() const;
182   int offset() const { return _offset; }
183   InlineKlass* inline_klass() const { return _inline_klass; }
184   LayoutKind layout_kind() const { return _layout_kind; }
185 
186   void release_handle();
187 
188   static unsigned get_hash(const JvmtiFlatTagMapKey& entry);
189   static bool equals(const JvmtiFlatTagMapKey& lhs, const JvmtiFlatTagMapKey& rhs);
190 };
191 
192 typedef
193 ResizeableHashTable <JvmtiFlatTagMapKey, jlong,
194                      AnyObj::C_HEAP, mtServiceability,
195                      JvmtiFlatTagMapKey::get_hash,
196                      JvmtiFlatTagMapKey::equals> FlatObjectHashtable;
197 
198 // A supporting class for iterating over all entries in JvmtiFlatTagMapTable.
199 class JvmtiFlatTagMapKeyClosure {
200 public:
201   virtual bool do_entry(JvmtiFlatTagMapKey& key, jlong& value) = 0;
202 };
203 
204 class JvmtiFlatTagMapTable: public CHeapObj<mtServiceability> {
205 private:
206   FlatObjectHashtable _table;
207 
208 public:
209   JvmtiFlatTagMapTable();
210   ~JvmtiFlatTagMapTable();
211 
212   int number_of_entries() const { return _table.number_of_entries(); }
213 
214   jlong find(const JvmtiHeapwalkObject& obj) const;
215   // obj must be flat
216   void add(const JvmtiHeapwalkObject& obj, jlong tag);
217 
218   // returns tag for the entry, 0 is not found
219   jlong remove(const JvmtiHeapwalkObject& obj);
220 
221   // iterate over entries in the hashmap
222   void entry_iterate(JvmtiFlatTagMapKeyClosure* closure);
223 
224   bool is_empty() const { return _table.number_of_entries() == 0; }
225 
226   void clear();
227 };
228 
229 #endif // SHARE_VM_PRIMS_TAGMAPTABLE_HPP