1 /*
  2  * Copyright (c) 1998, 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 #include "precompiled.hpp"
 26 #include "ci/ciEnv.hpp"
 27 #include "ci/ciInstance.hpp"
 28 #include "ci/ciMetadata.hpp"
 29 #include "ci/ciUtilities.hpp"
 30 #include "code/oopRecorder.inline.hpp"
 31 #include "gc/shared/collectedHeap.hpp"
 32 #include "memory/allocation.inline.hpp"
 33 #include "oops/oop.inline.hpp"
 34 #include "runtime/jniHandles.inline.hpp"
 35 #include "runtime/mutexLocker.hpp"
 36 #include "runtime/os.hpp"
 37 #include "utilities/copy.hpp"
 38 
 39 #ifdef ASSERT
 40 template <class T> int ValueRecorder<T>::_find_index_calls = 0;
 41 template <class T> int ValueRecorder<T>::_hit_indexes      = 0;
 42 template <class T> int ValueRecorder<T>::_missed_indexes   = 0;
 43 #endif //ASSERT
 44 
 45 
 46 template <class T> ValueRecorder<T>::ValueRecorder(Arena* arena) {
 47   _handles  = nullptr;
 48   _indexes  = nullptr;
 49   _arena    = arena;
 50   _complete = false;
 51 }
 52 
 53 template <class T> template <class X>  ValueRecorder<T>::IndexCache<X>::IndexCache() {
 54   assert(first_index > 0, "initial zero state of cache must be invalid index");
 55   Copy::zero_to_bytes(&_cache[0], sizeof(_cache));
 56 }
 57 
 58 template <class T> int ValueRecorder<T>::size() {
 59   _complete = true;
 60   if (_handles == nullptr)  return 0;
 61   return _handles->length() * sizeof(T);
 62 }
 63 
 64 template <class T> void ValueRecorder<T>::copy_values_to(nmethod* nm) {
 65   assert(_complete, "must be frozen");
 66   maybe_initialize();  // get non-null handles, even if we have no oops
 67   nm->copy_values(_handles);
 68 }
 69 
 70 template <class T> void ValueRecorder<T>::maybe_initialize() {
 71   if (_handles == nullptr) {
 72     if (_arena != nullptr) {
 73       _handles  = new(_arena) GrowableArray<T>(_arena, 10, 0, T{});
 74       _no_finds = new(_arena) GrowableArray<int>(_arena, 10, 0, 0);
 75     } else {
 76       _handles  = new GrowableArray<T>(10, 0, T{});
 77       _no_finds = new GrowableArray<int>(10, 0, 0);
 78     }
 79   }
 80 }
 81 
 82 
 83 template <class T> T ValueRecorder<T>::at(int index) {
 84   // there is always a nullptr virtually present as first object
 85   if (index == null_index)  return nullptr;
 86   return _handles->at(index - first_index);
 87 }
 88 
 89 
 90 template <class T> int ValueRecorder<T>::add_handle(T h, bool make_findable) {
 91   assert(!_complete, "cannot allocate more elements after size query");
 92   maybe_initialize();
 93   // indexing uses 1 as an origin--0 means null
 94   int index = _handles->length() + first_index;
 95   _handles->append(h);
 96 
 97   // Support correct operation of find_index().
 98   assert(!(make_findable && !is_real(h)), "nulls are not findable");
 99   if (make_findable) {
100     // This index may be returned from find_index().
101     if (_indexes != nullptr) {
102       int* cloc = _indexes->cache_location(h);
103       _indexes->set_cache_location_index(cloc, index);
104     } else if (index == index_cache_threshold && _arena != nullptr) {
105       _indexes = new(_arena) IndexCache<T>();
106       for (int i = 0; i < _handles->length(); i++) {
107         // Load the cache with pre-existing elements.
108         int index0 = i + first_index;
109         if (_no_finds->contains(index0))  continue;
110         int* cloc = _indexes->cache_location(_handles->at(i));
111         _indexes->set_cache_location_index(cloc, index0);
112       }
113     }
114   } else if (is_real(h)) {
115     // Remember that this index is not to be returned from find_index().
116     // This case is rare, because most or all uses of allocate_index pass
117     // an argument of nullptr or Universe::non_oop_word.
118     // Thus, the expected length of _no_finds is zero.
119     _no_finds->append(index);
120   }
121 
122   return index;
123 }
124 
125 
126 template <class T> int ValueRecorder<T>::maybe_find_index(T h) {
127   debug_only(_find_index_calls++);
128   assert(!_complete, "cannot allocate more elements after size query");
129   maybe_initialize();
130   if (h == nullptr)  return null_index;
131   assert(is_real(h), "must be valid");
132   int* cloc = (_indexes == nullptr)? nullptr: _indexes->cache_location(h);
133   if (cloc != nullptr) {
134     int cindex = _indexes->cache_location_index(cloc);
135     if (cindex == 0) {
136       return -1;   // We know this handle is completely new.
137     }
138     if (cindex >= first_index && _handles->at(cindex - first_index) == h) {
139       debug_only(_hit_indexes++);
140       return cindex;
141     }
142     if (!_indexes->cache_location_collision(cloc)) {
143       return -1;   // We know the current cache occupant is unique to that cloc.
144     }
145   }
146 
147   // Not found in cache, due to a cache collision.  (Or, no cache at all.)
148   // Do a linear search, most recent to oldest.
149   for (int i = _handles->length() - 1; i >= 0; i--) {
150     if (_handles->at(i) == h) {
151       int findex = i + first_index;
152       if (_no_finds->contains(findex))  continue;  // oops; skip this one
153       if (cloc != nullptr) {
154         _indexes->set_cache_location_index(cloc, findex);
155       }
156       debug_only(_missed_indexes++);
157       return findex;
158     }
159   }
160   return -1;
161 }
162 
163 // Explicitly instantiate these types
164 template class ValueRecorder<Metadata*>;
165 template class ValueRecorder<jobject>;
166 
167 oop ObjectLookup::ObjectEntry::oop_value() const { return JNIHandles::resolve(_value); }
168 
169 ObjectLookup::ObjectLookup(): _values(4), _gc_count(Universe::heap()->total_collections()) {}
170 
171 void ObjectLookup::maybe_resort() {
172   // The values are kept sorted by address which may be invalidated
173   // after a GC, so resort if a GC has occurred since last time.
174   if (_gc_count != Universe::heap()->total_collections()) {
175     _gc_count = Universe::heap()->total_collections();
176     _values.sort(sort_by_address);
177   }
178 }
179 
180 int ObjectLookup::sort_by_address(oop a, oop b) {
181   // oopDesc::compare returns the opposite of what this function returned
182   return -(oopDesc::compare(a, b));
183 }
184 
185 int ObjectLookup::sort_by_address(ObjectEntry* a, ObjectEntry* b) {
186   return sort_by_address(a->oop_value(), b->oop_value());
187 }
188 
189 int ObjectLookup::sort_oop_by_address(oop const& a, ObjectEntry const& b) {
190   return sort_by_address(a, b.oop_value());
191 }
192 
193 int ObjectLookup::find_index(jobject handle, OopRecorder* oop_recorder) {
194   if (handle == nullptr) {
195     return 0;
196   }
197   oop object = JNIHandles::resolve(handle);
198   maybe_resort();
199   bool found;
200   int location = _values.find_sorted<oop, sort_oop_by_address>(object, found);
201   if (!found) {
202     jobject handle = JNIHandles::make_local(object);
203     ObjectEntry r(handle, oop_recorder->allocate_oop_index(handle));
204     _values.insert_before(location, r);
205     return r.index();
206   }
207   return _values.at(location).index();
208 }
209 
210 OopRecorder::OopRecorder(Arena* arena, bool deduplicate): _oops(arena), _metadata(arena) {
211   if (deduplicate) {
212     _object_lookup = new ObjectLookup();
213   } else {
214     _object_lookup = nullptr;
215   }
216 }
217 
218 // Explicitly instantiate
219 template class ValueRecorder<address>;
220 
221 ExternalsRecorder* ExternalsRecorder::_recorder = nullptr;
222 
223 ExternalsRecorder::ExternalsRecorder(): _arena(mtCode), _externals(&_arena) {}
224 
225 #ifndef PRODUCT
226 static int total_access_count = 0;
227 static GrowableArray<int>* extern_hist = nullptr;
228 #endif
229 
230 void ExternalsRecorder_init() {
231   ExternalsRecorder::initialize();
232 }
233 
234 void ExternalsRecorder::initialize() {
235   // After Mutex and before CodeCache are initialized
236   assert(_recorder == nullptr, "should initialize only once");
237   _recorder = new ExternalsRecorder();
238 #ifndef PRODUCT
239   if (PrintNMethodStatistics) {
240     Arena* arena = &_recorder->_arena;
241     extern_hist = new(arena) GrowableArray<int>(arena, 512, 512, 0);
242   }
243 #endif
244 }
245 
246 int ExternalsRecorder::find_index(address adr) {
247   assert(_recorder != nullptr, "sanity");
248   MutexLocker ml(ExternalsRecorder_lock, Mutex::_no_safepoint_check_flag);
249   int index = _recorder->_externals.find_index(adr);
250 #ifndef PRODUCT
251   if (PrintNMethodStatistics) {
252     total_access_count++;
253     int n = extern_hist->at_grow(index, 0);
254     extern_hist->at_put(index, (n + 1));
255   }
256 #endif
257   return index;
258 }
259 
260 address ExternalsRecorder::at(int index) {
261   assert(_recorder != nullptr, "sanity");
262   // find_index() may resize array by reallocating it and freeing old,
263   // we need loock here to make sure we not accessing to old freed array.
264   MutexLocker ml(ExternalsRecorder_lock, Mutex::_no_safepoint_check_flag);
265   return _recorder->_externals.at(index);
266 }
267 
268 int ExternalsRecorder::count() {
269   assert(_recorder != nullptr, "sanity");
270   MutexLocker ml(ExternalsRecorder_lock, Mutex::_no_safepoint_check_flag);
271   return _recorder->_externals.count();
272 }
273 
274 #ifndef PRODUCT
275 extern "C" {
276   // Order from large to small values
277   static int count_cmp(const void *i, const void *j) {
278     int a = *(int*)i;
279     int b = *(int*)j;
280     return a < b ? 1 : a > b ? -1 : 0;
281   }
282 }
283 
284 void ExternalsRecorder::print_statistics() {
285   int cnt = count();
286   tty->print_cr("External addresses table: %d entries, %d accesses", cnt, total_access_count);
287   { // Print most accessed entries in the table.
288     int* array = NEW_C_HEAP_ARRAY(int, (2 * cnt), mtCode);
289     for (int i = 0; i < cnt; i++) {
290       array[(2 * i) + 0] = extern_hist->at(i);
291       array[(2 * i) + 1] = i;
292     }
293     // Reverse sort to have "hottest" addresses first.
294     qsort(array, cnt, 2*sizeof(int), count_cmp);
295     // Print all entries with Verbose flag otherwise only top 5.
296     int limit = (Verbose || cnt <= 5) ? cnt : 5;
297     int j = 0;
298     for (int i = 0; i < limit; i++) {
299       int index = array[(2 * i) + 1];
300       int n = extern_hist->at(index);
301       if (n > 0) {
302         address addr = at(index);
303         tty->print("%d: %8d " INTPTR_FORMAT " :", j++, n, p2i(addr));
304         if (addr != nullptr) {
305           if (is_card_table_address(addr)) {
306             tty->print(" card_table_base");
307           } else if (StubRoutines::contains(addr)) {
308             StubCodeDesc* desc = StubCodeDesc::desc_for(addr);
309             if (desc == nullptr) {
310               desc = StubCodeDesc::desc_for(addr + frame::pc_return_offset);
311             }
312             const char* stub_name = (desc != nullptr) ? desc->name() : "<unknown>";
313             tty->print(" stub: %s", stub_name);
314           } else {
315             ResourceMark rm;
316             const int buflen = 1024;
317             char* buf = NEW_RESOURCE_ARRAY(char, buflen);
318             int offset = 0;
319             if (os::dll_address_to_function_name(addr, buf, buflen, &offset)) {
320               tty->print(" extn: %s", buf);
321               if (offset != 0) {
322                 tty->print("+%d", offset);
323               }
324             } else {
325               if (CodeCache::contains((void*)addr)) {
326                 // Something in CodeCache
327                 tty->print(" in CodeCache");
328               } else {
329                 // It could be string
330                 memcpy(buf, (char*)addr, 80);
331                 buf[80] = '\0';
332                 tty->print(" '%s'", buf);
333               }
334             }
335           }
336         }
337         tty->cr();
338       }
339     }
340   }
341 }
342 #endif