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