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