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