1 /* 2 * Copyright (c) 1997, 2022, 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 "interpreter/oopMapCache.hpp" 27 #include "logging/log.hpp" 28 #include "logging/logStream.hpp" 29 #include "memory/allocation.inline.hpp" 30 #include "memory/resourceArea.hpp" 31 #include "oops/oop.inline.hpp" 32 #include "runtime/atomic.hpp" 33 #include "runtime/handles.inline.hpp" 34 #include "runtime/safepoint.hpp" 35 #include "runtime/signature.hpp" 36 37 class OopMapCacheEntry: private InterpreterOopMap { 38 friend class InterpreterOopMap; 39 friend class OopMapForCacheEntry; 40 friend class OopMapCache; 41 friend class VerifyClosure; 42 43 private: 44 OopMapCacheEntry* _next; 45 46 protected: 47 // Initialization 48 void fill(const methodHandle& method, int bci); 49 // fills the bit mask for native calls 50 void fill_for_native(const methodHandle& method); 51 void set_mask(CellTypeState* vars, CellTypeState* stack, int stack_top); 52 53 // Deallocate bit masks and initialize fields 54 void flush(); 55 56 private: 57 void allocate_bit_mask(); // allocates the bit mask on C heap f necessary 58 void deallocate_bit_mask(); // allocates the bit mask on C heap f necessary 59 bool verify_mask(CellTypeState *vars, CellTypeState *stack, int max_locals, int stack_top); 60 61 public: 62 OopMapCacheEntry() : InterpreterOopMap() { 63 _next = NULL; 64 #ifdef ASSERT 65 _resource_allocate_bit_mask = false; 66 #endif 67 } 68 }; 69 70 71 // Implementation of OopMapForCacheEntry 72 // (subclass of GenerateOopMap, initializes an OopMapCacheEntry for a given method and bci) 73 74 class OopMapForCacheEntry: public GenerateOopMap { 75 OopMapCacheEntry *_entry; 76 int _bci; 77 int _stack_top; 78 79 virtual bool report_results() const { return false; } 80 virtual bool possible_gc_point (BytecodeStream *bcs); 81 virtual void fill_stackmap_prolog (int nof_gc_points); 82 virtual void fill_stackmap_epilog (); 83 virtual void fill_stackmap_for_opcodes (BytecodeStream *bcs, 84 CellTypeState* vars, 85 CellTypeState* stack, 86 int stack_top); 87 virtual void fill_init_vars (GrowableArray<intptr_t> *init_vars); 88 89 public: 90 OopMapForCacheEntry(const methodHandle& method, int bci, OopMapCacheEntry *entry); 91 92 // Computes stack map for (method,bci) and initialize entry 93 bool compute_map(Thread* current); 94 int size(); 95 }; 96 97 98 OopMapForCacheEntry::OopMapForCacheEntry(const methodHandle& method, int bci, OopMapCacheEntry* entry) : GenerateOopMap(method) { 99 _bci = bci; 100 _entry = entry; 101 _stack_top = -1; 102 } 103 104 105 bool OopMapForCacheEntry::compute_map(Thread* current) { 106 assert(!method()->is_native(), "cannot compute oop map for native methods"); 107 // First check if it is a method where the stackmap is always empty 108 if (method()->code_size() == 0 || method()->max_locals() + method()->max_stack() == 0) { 109 _entry->set_mask_size(0); 110 } else { 111 ResourceMark rm; 112 if (!GenerateOopMap::compute_map(current)) { 113 fatal("Unrecoverable verification or out-of-memory error"); 114 return false; 115 } 116 result_for_basicblock(_bci); 117 } 118 return true; 119 } 120 121 122 bool OopMapForCacheEntry::possible_gc_point(BytecodeStream *bcs) { 123 return false; // We are not reporting any result. We call result_for_basicblock directly 124 } 125 126 127 void OopMapForCacheEntry::fill_stackmap_prolog(int nof_gc_points) { 128 // Do nothing 129 } 130 131 132 void OopMapForCacheEntry::fill_stackmap_epilog() { 133 // Do nothing 134 } 135 136 137 void OopMapForCacheEntry::fill_init_vars(GrowableArray<intptr_t> *init_vars) { 138 // Do nothing 139 } 140 141 142 void OopMapForCacheEntry::fill_stackmap_for_opcodes(BytecodeStream *bcs, 143 CellTypeState* vars, 144 CellTypeState* stack, 145 int stack_top) { 146 // Only interested in one specific bci 147 if (bcs->bci() == _bci) { 148 _entry->set_mask(vars, stack, stack_top); 149 _stack_top = stack_top; 150 } 151 } 152 153 154 int OopMapForCacheEntry::size() { 155 assert(_stack_top != -1, "compute_map must be called first"); 156 return ((method()->is_static()) ? 0 : 1) + method()->max_locals() + _stack_top; 157 } 158 159 160 // Implementation of InterpreterOopMap and OopMapCacheEntry 161 162 class VerifyClosure : public OffsetClosure { 163 private: 164 OopMapCacheEntry* _entry; 165 bool _failed; 166 167 public: 168 VerifyClosure(OopMapCacheEntry* entry) { _entry = entry; _failed = false; } 169 void offset_do(int offset) { if (!_entry->is_oop(offset)) _failed = true; } 170 bool failed() const { return _failed; } 171 }; 172 173 InterpreterOopMap::InterpreterOopMap() { 174 initialize(); 175 #ifdef ASSERT 176 _resource_allocate_bit_mask = true; 177 #endif 178 } 179 180 InterpreterOopMap::~InterpreterOopMap() { 181 // The expectation is that the bit mask was allocated 182 // last in this resource area. That would make the free of the 183 // bit_mask effective (see how FREE_RESOURCE_ARRAY does a free). 184 // If it was not allocated last, there is not a correctness problem 185 // but the space for the bit_mask is not freed. 186 assert(_resource_allocate_bit_mask, "Trying to free C heap space"); 187 if (mask_size() > small_mask_limit) { 188 FREE_RESOURCE_ARRAY(uintptr_t, _bit_mask[0], mask_word_size()); 189 } 190 } 191 192 bool InterpreterOopMap::is_empty() const { 193 bool result = _method == NULL; 194 assert(_method != NULL || (_bci == 0 && 195 (_mask_size == 0 || _mask_size == USHRT_MAX) && 196 _bit_mask[0] == 0), "Should be completely empty"); 197 return result; 198 } 199 200 void InterpreterOopMap::initialize() { 201 _method = NULL; 202 _mask_size = USHRT_MAX; // This value should cause a failure quickly 203 _bci = 0; 204 _expression_stack_size = 0; 205 _num_oops = 0; 206 for (int i = 0; i < N; i++) _bit_mask[i] = 0; 207 } 208 209 void InterpreterOopMap::iterate_oop(OffsetClosure* oop_closure) const { 210 int n = number_of_entries(); 211 int word_index = 0; 212 uintptr_t value = 0; 213 uintptr_t mask = 0; 214 // iterate over entries 215 for (int i = 0; i < n; i++, mask <<= bits_per_entry) { 216 // get current word 217 if (mask == 0) { 218 value = bit_mask()[word_index++]; 219 mask = 1; 220 } 221 // test for oop 222 if ((value & (mask << oop_bit_number)) != 0) oop_closure->offset_do(i); 223 } 224 } 225 226 void InterpreterOopMap::print() const { 227 int n = number_of_entries(); 228 tty->print("oop map for "); 229 method()->print_value(); 230 tty->print(" @ %d = [%d] { ", bci(), n); 231 for (int i = 0; i < n; i++) { 232 if (is_dead(i)) tty->print("%d+ ", i); 233 else 234 if (is_oop(i)) tty->print("%d ", i); 235 } 236 tty->print_cr("}"); 237 } 238 239 class MaskFillerForNative: public NativeSignatureIterator { 240 private: 241 uintptr_t * _mask; // the bit mask to be filled 242 int _size; // the mask size in bits 243 244 void set_one(int i) { 245 i *= InterpreterOopMap::bits_per_entry; 246 assert(0 <= i && i < _size, "offset out of bounds"); 247 _mask[i / BitsPerWord] |= (((uintptr_t) 1 << InterpreterOopMap::oop_bit_number) << (i % BitsPerWord)); 248 } 249 250 public: 251 void pass_byte() { /* ignore */ } 252 void pass_short() { /* ignore */ } 253 void pass_int() { /* ignore */ } 254 void pass_long() { /* ignore */ } 255 void pass_float() { /* ignore */ } 256 void pass_double() { /* ignore */ } 257 void pass_object() { set_one(offset()); } 258 259 MaskFillerForNative(const methodHandle& method, uintptr_t* mask, int size) : NativeSignatureIterator(method) { 260 _mask = mask; 261 _size = size; 262 // initialize with 0 263 int i = (size + BitsPerWord - 1) / BitsPerWord; 264 while (i-- > 0) _mask[i] = 0; 265 } 266 267 void generate() { 268 iterate(); 269 } 270 }; 271 272 bool OopMapCacheEntry::verify_mask(CellTypeState* vars, CellTypeState* stack, int max_locals, int stack_top) { 273 // Check mask includes map 274 VerifyClosure blk(this); 275 iterate_oop(&blk); 276 if (blk.failed()) return false; 277 278 // Check if map is generated correctly 279 // (Use ?: operator to make sure all 'true' & 'false' are represented exactly the same so we can use == afterwards) 280 const bool log = log_is_enabled(Trace, interpreter, oopmap); 281 LogStream st(Log(interpreter, oopmap)::trace()); 282 283 if (log) st.print("Locals (%d): ", max_locals); 284 for(int i = 0; i < max_locals; i++) { 285 bool v1 = is_oop(i) ? true : false; 286 bool v2 = vars[i].is_reference(); 287 assert(v1 == v2, "locals oop mask generation error"); 288 if (log) st.print("%d", v1 ? 1 : 0); 289 } 290 if (log) st.cr(); 291 292 if (log) st.print("Stack (%d): ", stack_top); 293 for(int j = 0; j < stack_top; j++) { 294 bool v1 = is_oop(max_locals + j) ? true : false; 295 bool v2 = stack[j].is_reference(); 296 assert(v1 == v2, "stack oop mask generation error"); 297 if (log) st.print("%d", v1 ? 1 : 0); 298 } 299 if (log) st.cr(); 300 return true; 301 } 302 303 void OopMapCacheEntry::allocate_bit_mask() { 304 if (mask_size() > small_mask_limit) { 305 assert(_bit_mask[0] == 0, "bit mask should be new or just flushed"); 306 _bit_mask[0] = (intptr_t) 307 NEW_C_HEAP_ARRAY(uintptr_t, mask_word_size(), mtClass); 308 } 309 } 310 311 void OopMapCacheEntry::deallocate_bit_mask() { 312 if (mask_size() > small_mask_limit && _bit_mask[0] != 0) { 313 assert(!Thread::current()->resource_area()->contains((void*)_bit_mask[0]), 314 "This bit mask should not be in the resource area"); 315 FREE_C_HEAP_ARRAY(uintptr_t, _bit_mask[0]); 316 debug_only(_bit_mask[0] = 0;) 317 } 318 } 319 320 321 void OopMapCacheEntry::fill_for_native(const methodHandle& mh) { 322 assert(mh->is_native(), "method must be native method"); 323 set_mask_size(mh->size_of_parameters() * bits_per_entry); 324 allocate_bit_mask(); 325 // fill mask for parameters 326 MaskFillerForNative mf(mh, bit_mask(), mask_size()); 327 mf.generate(); 328 } 329 330 331 void OopMapCacheEntry::fill(const methodHandle& method, int bci) { 332 // Flush entry to deallocate an existing entry 333 flush(); 334 set_method(method()); 335 set_bci(bci); 336 if (method->is_native()) { 337 // Native method activations have oops only among the parameters and one 338 // extra oop following the parameters (the mirror for static native methods). 339 fill_for_native(method); 340 } else { 341 OopMapForCacheEntry gen(method, bci, this); 342 if (!gen.compute_map(Thread::current())) { 343 fatal("Unrecoverable verification or out-of-memory error"); 344 } 345 } 346 } 347 348 349 void OopMapCacheEntry::set_mask(CellTypeState *vars, CellTypeState *stack, int stack_top) { 350 // compute bit mask size 351 int max_locals = method()->max_locals(); 352 int n_entries = max_locals + stack_top; 353 set_mask_size(n_entries * bits_per_entry); 354 allocate_bit_mask(); 355 set_expression_stack_size(stack_top); 356 357 // compute bits 358 int word_index = 0; 359 uintptr_t value = 0; 360 uintptr_t mask = 1; 361 362 _num_oops = 0; 363 CellTypeState* cell = vars; 364 for (int entry_index = 0; entry_index < n_entries; entry_index++, mask <<= bits_per_entry, cell++) { 365 // store last word 366 if (mask == 0) { 367 bit_mask()[word_index++] = value; 368 value = 0; 369 mask = 1; 370 } 371 372 // switch to stack when done with locals 373 if (entry_index == max_locals) { 374 cell = stack; 375 } 376 377 // set oop bit 378 if (cell->is_reference()) { 379 value |= (mask << oop_bit_number ); 380 _num_oops++; 381 } 382 383 // set dead bit 384 if (!cell->is_live()) { 385 value |= (mask << dead_bit_number); 386 assert(!cell->is_reference(), "dead value marked as oop"); 387 } 388 } 389 390 // make sure last word is stored 391 bit_mask()[word_index] = value; 392 393 // verify bit mask 394 assert(verify_mask(vars, stack, max_locals, stack_top), "mask could not be verified"); 395 } 396 397 void OopMapCacheEntry::flush() { 398 deallocate_bit_mask(); 399 initialize(); 400 } 401 402 403 // Implementation of OopMapCache 404 405 void InterpreterOopMap::resource_copy(OopMapCacheEntry* from) { 406 assert(_resource_allocate_bit_mask, 407 "Should not resource allocate the _bit_mask"); 408 409 set_method(from->method()); 410 set_bci(from->bci()); 411 set_mask_size(from->mask_size()); 412 set_expression_stack_size(from->expression_stack_size()); 413 _num_oops = from->num_oops(); 414 415 // Is the bit mask contained in the entry? 416 if (from->mask_size() <= small_mask_limit) { 417 memcpy((void *)_bit_mask, (void *)from->_bit_mask, 418 mask_word_size() * BytesPerWord); 419 } else { 420 // The expectation is that this InterpreterOopMap is a recently created 421 // and empty. It is used to get a copy of a cached entry. 422 // If the bit mask has a value, it should be in the 423 // resource area. 424 assert(_bit_mask[0] == 0 || 425 Thread::current()->resource_area()->contains((void*)_bit_mask[0]), 426 "The bit mask should have been allocated from a resource area"); 427 // Allocate the bit_mask from a Resource area for performance. Allocating 428 // from the C heap as is done for OopMapCache has a significant 429 // performance impact. 430 _bit_mask[0] = (uintptr_t) NEW_RESOURCE_ARRAY(uintptr_t, mask_word_size()); 431 assert(_bit_mask[0] != 0, "bit mask was not allocated"); 432 memcpy((void*) _bit_mask[0], (void*) from->_bit_mask[0], 433 mask_word_size() * BytesPerWord); 434 } 435 } 436 437 inline unsigned int OopMapCache::hash_value_for(const methodHandle& method, int bci) const { 438 // We use method->code_size() rather than method->identity_hash() below since 439 // the mark may not be present if a pointer to the method is already reversed. 440 return ((unsigned int) bci) 441 ^ ((unsigned int) method->max_locals() << 2) 442 ^ ((unsigned int) method->code_size() << 4) 443 ^ ((unsigned int) method->size_of_parameters() << 6); 444 } 445 446 OopMapCacheEntry* volatile OopMapCache::_old_entries = NULL; 447 448 OopMapCache::OopMapCache() { 449 _array = NEW_C_HEAP_ARRAY(OopMapCacheEntry*, _size, mtClass); 450 for(int i = 0; i < _size; i++) _array[i] = NULL; 451 } 452 453 454 OopMapCache::~OopMapCache() { 455 assert(_array != NULL, "sanity check"); 456 // Deallocate oop maps that are allocated out-of-line 457 flush(); 458 // Deallocate array 459 FREE_C_HEAP_ARRAY(OopMapCacheEntry*, _array); 460 } 461 462 OopMapCacheEntry* OopMapCache::entry_at(int i) const { 463 return Atomic::load_acquire(&(_array[i % _size])); 464 } 465 466 bool OopMapCache::put_at(int i, OopMapCacheEntry* entry, OopMapCacheEntry* old) { 467 return Atomic::cmpxchg(&_array[i % _size], old, entry) == old; 468 } 469 470 void OopMapCache::flush() { 471 for (int i = 0; i < _size; i++) { 472 OopMapCacheEntry* entry = _array[i]; 473 if (entry != NULL) { 474 _array[i] = NULL; // no barrier, only called in OopMapCache destructor 475 entry->flush(); 476 FREE_C_HEAP_OBJ(entry); 477 } 478 } 479 } 480 481 void OopMapCache::flush_obsolete_entries() { 482 assert(SafepointSynchronize::is_at_safepoint(), "called by RedefineClasses in a safepoint"); 483 for (int i = 0; i < _size; i++) { 484 OopMapCacheEntry* entry = _array[i]; 485 if (entry != NULL && !entry->is_empty() && entry->method()->is_old()) { 486 // Cache entry is occupied by an old redefined method and we don't want 487 // to pin it down so flush the entry. 488 if (log_is_enabled(Debug, redefine, class, oopmap)) { 489 ResourceMark rm; 490 log_debug(redefine, class, interpreter, oopmap) 491 ("flush: %s(%s): cached entry @%d", 492 entry->method()->name()->as_C_string(), entry->method()->signature()->as_C_string(), i); 493 } 494 _array[i] = NULL; 495 entry->flush(); 496 FREE_C_HEAP_OBJ(entry); 497 } 498 } 499 } 500 501 // Called by GC for thread root scan during a safepoint only. The other interpreted frame oopmaps 502 // are generated locally and not cached. 503 void OopMapCache::lookup(const methodHandle& method, 504 int bci, 505 InterpreterOopMap* entry_for) { 506 assert(SafepointSynchronize::is_at_safepoint(), "called by GC in a safepoint"); 507 int probe = hash_value_for(method, bci); 508 int i; 509 OopMapCacheEntry* entry = NULL; 510 511 if (log_is_enabled(Debug, interpreter, oopmap)) { 512 static int count = 0; 513 ResourceMark rm; 514 log_debug(interpreter, oopmap) 515 ("%d - Computing oopmap at bci %d for %s at hash %d", ++count, bci, 516 method()->name_and_sig_as_C_string(), probe); 517 } 518 519 // Search hashtable for match 520 for(i = 0; i < _probe_depth; i++) { 521 entry = entry_at(probe + i); 522 if (entry != NULL && !entry->is_empty() && entry->match(method, bci)) { 523 entry_for->resource_copy(entry); 524 assert(!entry_for->is_empty(), "A non-empty oop map should be returned"); 525 log_debug(interpreter, oopmap)("- found at hash %d", probe + i); 526 return; 527 } 528 } 529 530 // Entry is not in hashtable. 531 // Compute entry 532 533 OopMapCacheEntry* tmp = NEW_C_HEAP_OBJ(OopMapCacheEntry, mtClass); 534 tmp->initialize(); 535 tmp->fill(method, bci); 536 entry_for->resource_copy(tmp); 537 538 if (method->should_not_be_cached()) { 539 // It is either not safe or not a good idea to cache this Method* 540 // at this time. We give the caller of lookup() a copy of the 541 // interesting info via parameter entry_for, but we don't add it to 542 // the cache. See the gory details in Method*.cpp. 543 FREE_C_HEAP_OBJ(tmp); 544 return; 545 } 546 547 // First search for an empty slot 548 for(i = 0; i < _probe_depth; i++) { 549 entry = entry_at(probe + i); 550 if (entry == NULL) { 551 if (put_at(probe + i, tmp, NULL)) { 552 assert(!entry_for->is_empty(), "A non-empty oop map should be returned"); 553 return; 554 } 555 } 556 } 557 558 log_debug(interpreter, oopmap)("*** collision in oopmap cache - flushing item ***"); 559 560 // No empty slot (uncommon case). Use (some approximation of a) LRU algorithm 561 // where the first entry in the collision array is replaced with the new one. 562 OopMapCacheEntry* old = entry_at(probe + 0); 563 if (put_at(probe + 0, tmp, old)) { 564 enqueue_for_cleanup(old); 565 } else { 566 enqueue_for_cleanup(tmp); 567 } 568 569 assert(!entry_for->is_empty(), "A non-empty oop map should be returned"); 570 return; 571 } 572 573 void OopMapCache::enqueue_for_cleanup(OopMapCacheEntry* entry) { 574 bool success = false; 575 OopMapCacheEntry* head; 576 do { 577 head = _old_entries; 578 entry->_next = head; 579 success = Atomic::cmpxchg(&_old_entries, head, entry) == head; 580 } while (!success); 581 582 if (log_is_enabled(Debug, interpreter, oopmap)) { 583 ResourceMark rm; 584 log_debug(interpreter, oopmap)("enqueue %s at bci %d for cleanup", 585 entry->method()->name_and_sig_as_C_string(), entry->bci()); 586 } 587 } 588 589 // This is called after GC threads are done and nothing is accessing the old_entries 590 // list, so no synchronization needed. 591 void OopMapCache::cleanup_old_entries() { 592 OopMapCacheEntry* entry = _old_entries; 593 _old_entries = NULL; 594 while (entry != NULL) { 595 if (log_is_enabled(Debug, interpreter, oopmap)) { 596 ResourceMark rm; 597 log_debug(interpreter, oopmap)("cleanup entry %s at bci %d", 598 entry->method()->name_and_sig_as_C_string(), entry->bci()); 599 } 600 OopMapCacheEntry* next = entry->_next; 601 entry->flush(); 602 FREE_C_HEAP_OBJ(entry); 603 entry = next; 604 } 605 } 606 607 void OopMapCache::compute_one_oop_map(const methodHandle& method, int bci, InterpreterOopMap* entry) { 608 // Due to the invariants above it's tricky to allocate a temporary OopMapCacheEntry on the stack 609 OopMapCacheEntry* tmp = NEW_C_HEAP_OBJ(OopMapCacheEntry, mtClass); 610 tmp->initialize(); 611 tmp->fill(method, bci); 612 entry->resource_copy(tmp); 613 FREE_C_HEAP_OBJ(tmp); 614 }