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 expection 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 for (int i = 0; i < N; i++) _bit_mask[i] = 0; 206 } 207 208 void InterpreterOopMap::iterate_oop(OffsetClosure* oop_closure) const { 209 int n = number_of_entries(); 210 int word_index = 0; 211 uintptr_t value = 0; 212 uintptr_t mask = 0; 213 // iterate over entries 214 for (int i = 0; i < n; i++, mask <<= bits_per_entry) { 215 // get current word 216 if (mask == 0) { 217 value = bit_mask()[word_index++]; 218 mask = 1; 219 } 220 // test for oop 221 if ((value & (mask << oop_bit_number)) != 0) oop_closure->offset_do(i); 222 } 223 } 224 225 void InterpreterOopMap::print() const { 226 int n = number_of_entries(); 227 tty->print("oop map for "); 228 method()->print_value(); 229 tty->print(" @ %d = [%d] { ", bci(), n); 230 for (int i = 0; i < n; i++) { 231 if (is_dead(i)) tty->print("%d+ ", i); 232 else 233 if (is_oop(i)) tty->print("%d ", i); 234 } 235 tty->print_cr("}"); 236 } 237 238 class MaskFillerForNative: public NativeSignatureIterator { 239 private: 240 uintptr_t * _mask; // the bit mask to be filled 241 int _size; // the mask size in bits 242 243 void set_one(int i) { 244 i *= InterpreterOopMap::bits_per_entry; 245 assert(0 <= i && i < _size, "offset out of bounds"); 246 _mask[i / BitsPerWord] |= (((uintptr_t) 1 << InterpreterOopMap::oop_bit_number) << (i % BitsPerWord)); 247 } 248 249 public: 250 void pass_byte() { /* ignore */ } 251 void pass_short() { /* ignore */ } 252 void pass_int() { /* ignore */ } 253 void pass_long() { /* ignore */ } 254 void pass_float() { /* ignore */ } 255 void pass_double() { /* ignore */ } 256 void pass_object() { set_one(offset()); } 257 258 MaskFillerForNative(const methodHandle& method, uintptr_t* mask, int size) : NativeSignatureIterator(method) { 259 _mask = mask; 260 _size = size; 261 // initialize with 0 262 int i = (size + BitsPerWord - 1) / BitsPerWord; 263 while (i-- > 0) _mask[i] = 0; 264 } 265 266 void generate() { 267 iterate(); 268 } 269 }; 270 271 bool OopMapCacheEntry::verify_mask(CellTypeState* vars, CellTypeState* stack, int max_locals, int stack_top) { 272 // Check mask includes map 273 VerifyClosure blk(this); 274 iterate_oop(&blk); 275 if (blk.failed()) return false; 276 277 // Check if map is generated correctly 278 // (Use ?: operator to make sure all 'true' & 'false' are represented exactly the same so we can use == afterwards) 279 const bool log = log_is_enabled(Trace, interpreter, oopmap); 280 LogStream st(Log(interpreter, oopmap)::trace()); 281 282 if (log) st.print("Locals (%d): ", max_locals); 283 for(int i = 0; i < max_locals; i++) { 284 bool v1 = is_oop(i) ? true : false; 285 bool v2 = vars[i].is_reference(); 286 assert(v1 == v2, "locals oop mask generation error"); 287 if (log) st.print("%d", v1 ? 1 : 0); 288 } 289 if (log) st.cr(); 290 291 if (log) st.print("Stack (%d): ", stack_top); 292 for(int j = 0; j < stack_top; j++) { 293 bool v1 = is_oop(max_locals + j) ? true : false; 294 bool v2 = stack[j].is_reference(); 295 assert(v1 == v2, "stack oop mask generation error"); 296 if (log) st.print("%d", v1 ? 1 : 0); 297 } 298 if (log) st.cr(); 299 return true; 300 } 301 302 void OopMapCacheEntry::allocate_bit_mask() { 303 if (mask_size() > small_mask_limit) { 304 assert(_bit_mask[0] == 0, "bit mask should be new or just flushed"); 305 _bit_mask[0] = (intptr_t) 306 NEW_C_HEAP_ARRAY(uintptr_t, mask_word_size(), mtClass); 307 } 308 } 309 310 void OopMapCacheEntry::deallocate_bit_mask() { 311 if (mask_size() > small_mask_limit && _bit_mask[0] != 0) { 312 assert(!Thread::current()->resource_area()->contains((void*)_bit_mask[0]), 313 "This bit mask should not be in the resource area"); 314 FREE_C_HEAP_ARRAY(uintptr_t, _bit_mask[0]); 315 debug_only(_bit_mask[0] = 0;) 316 } 317 } 318 319 320 void OopMapCacheEntry::fill_for_native(const methodHandle& mh) { 321 assert(mh->is_native(), "method must be native method"); 322 set_mask_size(mh->size_of_parameters() * bits_per_entry); 323 allocate_bit_mask(); 324 // fill mask for parameters 325 MaskFillerForNative mf(mh, bit_mask(), mask_size()); 326 mf.generate(); 327 } 328 329 330 void OopMapCacheEntry::fill(const methodHandle& method, int bci) { 331 // Flush entry to deallocate an existing entry 332 flush(); 333 set_method(method()); 334 set_bci(bci); 335 if (method->is_native()) { 336 // Native method activations have oops only among the parameters and one 337 // extra oop following the parameters (the mirror for static native methods). 338 fill_for_native(method); 339 } else { 340 OopMapForCacheEntry gen(method, bci, this); 341 if (!gen.compute_map(Thread::current())) { 342 fatal("Unrecoverable verification or out-of-memory error"); 343 } 344 } 345 } 346 347 348 void OopMapCacheEntry::set_mask(CellTypeState *vars, CellTypeState *stack, int stack_top) { 349 // compute bit mask size 350 int max_locals = method()->max_locals(); 351 int n_entries = max_locals + stack_top; 352 set_mask_size(n_entries * bits_per_entry); 353 allocate_bit_mask(); 354 set_expression_stack_size(stack_top); 355 356 // compute bits 357 int word_index = 0; 358 uintptr_t value = 0; 359 uintptr_t mask = 1; 360 361 CellTypeState* cell = vars; 362 for (int entry_index = 0; entry_index < n_entries; entry_index++, mask <<= bits_per_entry, cell++) { 363 // store last word 364 if (mask == 0) { 365 bit_mask()[word_index++] = value; 366 value = 0; 367 mask = 1; 368 } 369 370 // switch to stack when done with locals 371 if (entry_index == max_locals) { 372 cell = stack; 373 } 374 375 // set oop bit 376 if (cell->is_reference()) { 377 value |= (mask << oop_bit_number ); 378 } 379 380 // set dead bit 381 if (!cell->is_live()) { 382 value |= (mask << dead_bit_number); 383 assert(!cell->is_reference(), "dead value marked as oop"); 384 } 385 } 386 387 // make sure last word is stored 388 bit_mask()[word_index] = value; 389 390 // verify bit mask 391 assert(verify_mask(vars, stack, max_locals, stack_top), "mask could not be verified"); 392 } 393 394 void OopMapCacheEntry::flush() { 395 deallocate_bit_mask(); 396 initialize(); 397 } 398 399 400 // Implementation of OopMapCache 401 402 void InterpreterOopMap::resource_copy(OopMapCacheEntry* from) { 403 assert(_resource_allocate_bit_mask, 404 "Should not resource allocate the _bit_mask"); 405 406 set_method(from->method()); 407 set_bci(from->bci()); 408 set_mask_size(from->mask_size()); 409 set_expression_stack_size(from->expression_stack_size()); 410 411 // Is the bit mask contained in the entry? 412 if (from->mask_size() <= small_mask_limit) { 413 memcpy((void *)_bit_mask, (void *)from->_bit_mask, 414 mask_word_size() * BytesPerWord); 415 } else { 416 // The expectation is that this InterpreterOopMap is a recently created 417 // and empty. It is used to get a copy of a cached entry. 418 // If the bit mask has a value, it should be in the 419 // resource area. 420 assert(_bit_mask[0] == 0 || 421 Thread::current()->resource_area()->contains((void*)_bit_mask[0]), 422 "The bit mask should have been allocated from a resource area"); 423 // Allocate the bit_mask from a Resource area for performance. Allocating 424 // from the C heap as is done for OopMapCache has a significant 425 // performance impact. 426 _bit_mask[0] = (uintptr_t) NEW_RESOURCE_ARRAY(uintptr_t, mask_word_size()); 427 assert(_bit_mask[0] != 0, "bit mask was not allocated"); 428 memcpy((void*) _bit_mask[0], (void*) from->_bit_mask[0], 429 mask_word_size() * BytesPerWord); 430 } 431 } 432 433 inline unsigned int OopMapCache::hash_value_for(const methodHandle& method, int bci) const { 434 // We use method->code_size() rather than method->identity_hash() below since 435 // the mark may not be present if a pointer to the method is already reversed. 436 return ((unsigned int) bci) 437 ^ ((unsigned int) method->max_locals() << 2) 438 ^ ((unsigned int) method->code_size() << 4) 439 ^ ((unsigned int) method->size_of_parameters() << 6); 440 } 441 442 OopMapCacheEntry* volatile OopMapCache::_old_entries = NULL; 443 444 OopMapCache::OopMapCache() { 445 _array = NEW_C_HEAP_ARRAY(OopMapCacheEntry*, _size, mtClass); 446 for(int i = 0; i < _size; i++) _array[i] = NULL; 447 } 448 449 450 OopMapCache::~OopMapCache() { 451 assert(_array != NULL, "sanity check"); 452 // Deallocate oop maps that are allocated out-of-line 453 flush(); 454 // Deallocate array 455 FREE_C_HEAP_ARRAY(OopMapCacheEntry*, _array); 456 } 457 458 OopMapCacheEntry* OopMapCache::entry_at(int i) const { 459 return Atomic::load_acquire(&(_array[i % _size])); 460 } 461 462 bool OopMapCache::put_at(int i, OopMapCacheEntry* entry, OopMapCacheEntry* old) { 463 return Atomic::cmpxchg(&_array[i % _size], old, entry) == old; 464 } 465 466 void OopMapCache::flush() { 467 for (int i = 0; i < _size; i++) { 468 OopMapCacheEntry* entry = _array[i]; 469 if (entry != NULL) { 470 _array[i] = NULL; // no barrier, only called in OopMapCache destructor 471 entry->flush(); 472 FREE_C_HEAP_OBJ(entry); 473 } 474 } 475 } 476 477 void OopMapCache::flush_obsolete_entries() { 478 assert(SafepointSynchronize::is_at_safepoint(), "called by RedefineClasses in a safepoint"); 479 for (int i = 0; i < _size; i++) { 480 OopMapCacheEntry* entry = _array[i]; 481 if (entry != NULL && !entry->is_empty() && entry->method()->is_old()) { 482 // Cache entry is occupied by an old redefined method and we don't want 483 // to pin it down so flush the entry. 484 if (log_is_enabled(Debug, redefine, class, oopmap)) { 485 ResourceMark rm; 486 log_debug(redefine, class, interpreter, oopmap) 487 ("flush: %s(%s): cached entry @%d", 488 entry->method()->name()->as_C_string(), entry->method()->signature()->as_C_string(), i); 489 } 490 _array[i] = NULL; 491 entry->flush(); 492 FREE_C_HEAP_OBJ(entry); 493 } 494 } 495 } 496 497 // Called by GC for thread root scan during a safepoint only. The other interpreted frame oopmaps 498 // are generated locally and not cached. 499 void OopMapCache::lookup(const methodHandle& method, 500 int bci, 501 InterpreterOopMap* entry_for) { 502 assert(SafepointSynchronize::is_at_safepoint(), "called by GC in a safepoint"); 503 int probe = hash_value_for(method, bci); 504 int i; 505 OopMapCacheEntry* entry = NULL; 506 507 if (log_is_enabled(Debug, interpreter, oopmap)) { 508 static int count = 0; 509 ResourceMark rm; 510 log_debug(interpreter, oopmap) 511 ("%d - Computing oopmap at bci %d for %s at hash %d", ++count, bci, 512 method()->name_and_sig_as_C_string(), probe); 513 } 514 515 // Search hashtable for match 516 for(i = 0; i < _probe_depth; i++) { 517 entry = entry_at(probe + i); 518 if (entry != NULL && !entry->is_empty() && entry->match(method, bci)) { 519 entry_for->resource_copy(entry); 520 assert(!entry_for->is_empty(), "A non-empty oop map should be returned"); 521 log_debug(interpreter, oopmap)("- found at hash %d", probe + i); 522 return; 523 } 524 } 525 526 // Entry is not in hashtable. 527 // Compute entry 528 529 OopMapCacheEntry* tmp = NEW_C_HEAP_OBJ(OopMapCacheEntry, mtClass); 530 tmp->initialize(); 531 tmp->fill(method, bci); 532 entry_for->resource_copy(tmp); 533 534 if (method->should_not_be_cached()) { 535 // It is either not safe or not a good idea to cache this Method* 536 // at this time. We give the caller of lookup() a copy of the 537 // interesting info via parameter entry_for, but we don't add it to 538 // the cache. See the gory details in Method*.cpp. 539 FREE_C_HEAP_OBJ(tmp); 540 return; 541 } 542 543 // First search for an empty slot 544 for(i = 0; i < _probe_depth; i++) { 545 entry = entry_at(probe + i); 546 if (entry == NULL) { 547 if (put_at(probe + i, tmp, NULL)) { 548 assert(!entry_for->is_empty(), "A non-empty oop map should be returned"); 549 return; 550 } 551 } 552 } 553 554 log_debug(interpreter, oopmap)("*** collision in oopmap cache - flushing item ***"); 555 556 // No empty slot (uncommon case). Use (some approximation of a) LRU algorithm 557 // where the first entry in the collision array is replaced with the new one. 558 OopMapCacheEntry* old = entry_at(probe + 0); 559 if (put_at(probe + 0, tmp, old)) { 560 enqueue_for_cleanup(old); 561 } else { 562 enqueue_for_cleanup(tmp); 563 } 564 565 assert(!entry_for->is_empty(), "A non-empty oop map should be returned"); 566 return; 567 } 568 569 void OopMapCache::enqueue_for_cleanup(OopMapCacheEntry* entry) { 570 bool success = false; 571 OopMapCacheEntry* head; 572 do { 573 head = _old_entries; 574 entry->_next = head; 575 success = Atomic::cmpxchg(&_old_entries, head, entry) == head; 576 } while (!success); 577 578 if (log_is_enabled(Debug, interpreter, oopmap)) { 579 ResourceMark rm; 580 log_debug(interpreter, oopmap)("enqueue %s at bci %d for cleanup", 581 entry->method()->name_and_sig_as_C_string(), entry->bci()); 582 } 583 } 584 585 // This is called after GC threads are done and nothing is accessing the old_entries 586 // list, so no synchronization needed. 587 void OopMapCache::cleanup_old_entries() { 588 OopMapCacheEntry* entry = _old_entries; 589 _old_entries = NULL; 590 while (entry != NULL) { 591 if (log_is_enabled(Debug, interpreter, oopmap)) { 592 ResourceMark rm; 593 log_debug(interpreter, oopmap)("cleanup entry %s at bci %d", 594 entry->method()->name_and_sig_as_C_string(), entry->bci()); 595 } 596 OopMapCacheEntry* next = entry->_next; 597 entry->flush(); 598 FREE_C_HEAP_OBJ(entry); 599 entry = next; 600 } 601 } 602 603 void OopMapCache::compute_one_oop_map(const methodHandle& method, int bci, InterpreterOopMap* entry) { 604 // Due to the invariants above it's tricky to allocate a temporary OopMapCacheEntry on the stack 605 OopMapCacheEntry* tmp = NEW_C_HEAP_OBJ(OopMapCacheEntry, mtClass); 606 tmp->initialize(); 607 tmp->fill(method, bci); 608 entry->resource_copy(tmp); 609 FREE_C_HEAP_OBJ(tmp); 610 }