1 /* 2 * Copyright (c) 1997, 2018, 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 "code/codeBlob.hpp" 27 #include "code/codeCache.hpp" 28 #include "code/compiledIC.hpp" 29 #include "code/dependencies.hpp" 30 #include "code/icBuffer.hpp" 31 #include "code/nmethod.hpp" 32 #include "code/pcDesc.hpp" 33 #include "compiler/compileBroker.hpp" 34 #include "gc_implementation/shared/markSweep.hpp" 35 #include "jfr/jfrEvents.hpp" 36 #include "memory/allocation.inline.hpp" 37 #include "memory/gcLocker.hpp" 38 #include "memory/iterator.hpp" 39 #include "memory/resourceArea.hpp" 40 #include "oops/method.hpp" 41 #include "oops/objArrayOop.hpp" 42 #include "oops/oop.inline.hpp" 43 #include "runtime/handles.inline.hpp" 44 #include "runtime/arguments.hpp" 45 #include "runtime/deoptimization.hpp" 46 #include "runtime/icache.hpp" 47 #include "runtime/java.hpp" 48 #include "runtime/mutexLocker.hpp" 49 #include "services/memoryService.hpp" 50 #include "utilities/xmlstream.hpp" 51 52 53 // Helper class for printing in CodeCache 54 55 class CodeBlob_sizes { 56 private: 57 int count; 58 int total_size; 59 int header_size; 60 int code_size; 61 int stub_size; 62 int relocation_size; 63 int scopes_oop_size; 64 int scopes_metadata_size; 65 int scopes_data_size; 66 int scopes_pcs_size; 67 68 public: 69 CodeBlob_sizes() { 70 count = 0; 71 total_size = 0; 72 header_size = 0; 73 code_size = 0; 74 stub_size = 0; 75 relocation_size = 0; 76 scopes_oop_size = 0; 77 scopes_metadata_size = 0; 78 scopes_data_size = 0; 79 scopes_pcs_size = 0; 80 } 81 82 int total() { return total_size; } 83 bool is_empty() { return count == 0; } 84 85 void print(const char* title) { 86 tty->print_cr(" #%d %s = %dK (hdr %d%%, loc %d%%, code %d%%, stub %d%%, [oops %d%%, metadata %d%%, data %d%%, pcs %d%%])", 87 count, 88 title, 89 (int)(total() / K), 90 header_size * 100 / total_size, 91 relocation_size * 100 / total_size, 92 code_size * 100 / total_size, 93 stub_size * 100 / total_size, 94 scopes_oop_size * 100 / total_size, 95 scopes_metadata_size * 100 / total_size, 96 scopes_data_size * 100 / total_size, 97 scopes_pcs_size * 100 / total_size); 98 } 99 100 void add(CodeBlob* cb) { 101 count++; 102 total_size += cb->size(); 103 header_size += cb->header_size(); 104 relocation_size += cb->relocation_size(); 105 if (cb->is_nmethod()) { 106 nmethod* nm = cb->as_nmethod_or_null(); 107 code_size += nm->insts_size(); 108 stub_size += nm->stub_size(); 109 110 scopes_oop_size += nm->oops_size(); 111 scopes_metadata_size += nm->metadata_size(); 112 scopes_data_size += nm->scopes_data_size(); 113 scopes_pcs_size += nm->scopes_pcs_size(); 114 } else { 115 code_size += cb->code_size(); 116 } 117 } 118 }; 119 120 // CodeCache implementation 121 122 CodeHeap * CodeCache::_heap = new CodeHeap(); 123 int CodeCache::_number_of_blobs = 0; 124 int CodeCache::_number_of_adapters = 0; 125 int CodeCache::_number_of_nmethods = 0; 126 int CodeCache::_number_of_nmethods_with_dependencies = 0; 127 bool CodeCache::_needs_cache_clean = false; 128 nmethod* CodeCache::_scavenge_root_nmethods = NULL; 129 130 int CodeCache::_codemem_full_count = 0; 131 132 CodeBlob* CodeCache::first() { 133 assert_locked_or_safepoint(CodeCache_lock); 134 return (CodeBlob*)_heap->first(); 135 } 136 137 138 CodeBlob* CodeCache::next(CodeBlob* cb) { 139 assert_locked_or_safepoint(CodeCache_lock); 140 return (CodeBlob*)_heap->next(cb); 141 } 142 143 144 CodeBlob* CodeCache::alive(CodeBlob *cb) { 145 assert_locked_or_safepoint(CodeCache_lock); 146 while (cb != NULL && !cb->is_alive()) cb = next(cb); 147 return cb; 148 } 149 150 151 nmethod* CodeCache::alive_nmethod(CodeBlob* cb) { 152 assert_locked_or_safepoint(CodeCache_lock); 153 while (cb != NULL && (!cb->is_alive() || !cb->is_nmethod())) cb = next(cb); 154 return (nmethod*)cb; 155 } 156 157 nmethod* CodeCache::first_nmethod() { 158 assert_locked_or_safepoint(CodeCache_lock); 159 CodeBlob* cb = first(); 160 while (cb != NULL && !cb->is_nmethod()) { 161 cb = next(cb); 162 } 163 return (nmethod*)cb; 164 } 165 166 nmethod* CodeCache::next_nmethod (CodeBlob* cb) { 167 assert_locked_or_safepoint(CodeCache_lock); 168 cb = next(cb); 169 while (cb != NULL && !cb->is_nmethod()) { 170 cb = next(cb); 171 } 172 return (nmethod*)cb; 173 } 174 175 static size_t maxCodeCacheUsed = 0; 176 177 CodeBlob* CodeCache::allocate(int size, bool is_critical) { 178 // Do not seize the CodeCache lock here--if the caller has not 179 // already done so, we are going to lose bigtime, since the code 180 // cache will contain a garbage CodeBlob until the caller can 181 // run the constructor for the CodeBlob subclass he is busy 182 // instantiating. 183 guarantee(size >= 0, "allocation request must be reasonable"); 184 assert_locked_or_safepoint(CodeCache_lock); 185 CodeBlob* cb = NULL; 186 _number_of_blobs++; 187 while (true) { 188 cb = (CodeBlob*)_heap->allocate(size, is_critical); 189 if (cb != NULL) break; 190 if (!_heap->expand_by(CodeCacheExpansionSize)) { 191 // Expansion failed 192 return NULL; 193 } 194 if (PrintCodeCacheExtension) { 195 ResourceMark rm; 196 tty->print_cr("code cache extended to [" INTPTR_FORMAT ", " INTPTR_FORMAT "] (" SSIZE_FORMAT " bytes)", 197 (intptr_t)_heap->low_boundary(), (intptr_t)_heap->high(), 198 (address)_heap->high() - (address)_heap->low_boundary()); 199 } 200 } 201 maxCodeCacheUsed = MAX2(maxCodeCacheUsed, ((address)_heap->high_boundary() - 202 (address)_heap->low_boundary()) - unallocated_capacity()); 203 verify_if_often(); 204 print_trace("allocation", cb, size); 205 return cb; 206 } 207 208 void CodeCache::free(CodeBlob* cb) { 209 assert_locked_or_safepoint(CodeCache_lock); 210 verify_if_often(); 211 212 print_trace("free", cb); 213 if (cb->is_nmethod()) { 214 _number_of_nmethods--; 215 if (((nmethod *)cb)->has_dependencies()) { 216 _number_of_nmethods_with_dependencies--; 217 } 218 } 219 if (cb->is_adapter_blob()) { 220 _number_of_adapters--; 221 } 222 _number_of_blobs--; 223 224 _heap->deallocate(cb); 225 226 verify_if_often(); 227 assert(_number_of_blobs >= 0, "sanity check"); 228 } 229 230 231 void CodeCache::commit(CodeBlob* cb) { 232 // this is called by nmethod::nmethod, which must already own CodeCache_lock 233 assert_locked_or_safepoint(CodeCache_lock); 234 if (cb->is_nmethod()) { 235 _number_of_nmethods++; 236 if (((nmethod *)cb)->has_dependencies()) { 237 _number_of_nmethods_with_dependencies++; 238 } 239 } 240 if (cb->is_adapter_blob()) { 241 _number_of_adapters++; 242 } 243 244 // flush the hardware I-cache 245 ICache::invalidate_range(cb->content_begin(), cb->content_size()); 246 } 247 248 249 void CodeCache::flush() { 250 assert_locked_or_safepoint(CodeCache_lock); 251 Unimplemented(); 252 } 253 254 255 // Iteration over CodeBlobs 256 257 #define FOR_ALL_BLOBS(var) for (CodeBlob *var = first() ; var != NULL; var = next(var) ) 258 #define FOR_ALL_ALIVE_BLOBS(var) for (CodeBlob *var = alive(first()); var != NULL; var = alive(next(var))) 259 #define FOR_ALL_ALIVE_NMETHODS(var) for (nmethod *var = alive_nmethod(first()); var != NULL; var = alive_nmethod(next(var))) 260 261 262 bool CodeCache::contains(void *p) { 263 // It should be ok to call contains without holding a lock 264 return _heap->contains(p); 265 } 266 267 268 // This method is safe to call without holding the CodeCache_lock, as long as a dead codeblob is not 269 // looked up (i.e., one that has been marked for deletion). It only dependes on the _segmap to contain 270 // valid indices, which it will always do, as long as the CodeBlob is not in the process of being recycled. 271 CodeBlob* CodeCache::find_blob(void* start) { 272 CodeBlob* result = find_blob_unsafe(start); 273 if (result == NULL) return NULL; 274 // We could potientially look up non_entrant methods 275 guarantee(!result->is_zombie() || result->is_locked_by_vm() || is_error_reported(), "unsafe access to zombie method"); 276 return result; 277 } 278 279 nmethod* CodeCache::find_nmethod(void* start) { 280 CodeBlob *cb = find_blob(start); 281 assert(cb == NULL || cb->is_nmethod(), "did not find an nmethod"); 282 return (nmethod*)cb; 283 } 284 285 286 void CodeCache::blobs_do(void f(CodeBlob* nm)) { 287 assert_locked_or_safepoint(CodeCache_lock); 288 FOR_ALL_BLOBS(p) { 289 f(p); 290 } 291 } 292 293 294 void CodeCache::nmethods_do(void f(nmethod* nm)) { 295 assert_locked_or_safepoint(CodeCache_lock); 296 FOR_ALL_BLOBS(nm) { 297 if (nm->is_nmethod()) f((nmethod*)nm); 298 } 299 } 300 301 void CodeCache::alive_nmethods_do(void f(nmethod* nm)) { 302 assert_locked_or_safepoint(CodeCache_lock); 303 FOR_ALL_ALIVE_NMETHODS(nm) { 304 f(nm); 305 } 306 } 307 308 int CodeCache::alignment_unit() { 309 return (int)_heap->alignment_unit(); 310 } 311 312 313 int CodeCache::alignment_offset() { 314 return (int)_heap->alignment_offset(); 315 } 316 317 318 // Mark nmethods for unloading if they contain otherwise unreachable 319 // oops. 320 void CodeCache::do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred) { 321 assert_locked_or_safepoint(CodeCache_lock); 322 FOR_ALL_ALIVE_NMETHODS(nm) { 323 nm->do_unloading(is_alive, unloading_occurred); 324 } 325 } 326 327 void CodeCache::blobs_do(CodeBlobClosure* f) { 328 assert_locked_or_safepoint(CodeCache_lock); 329 FOR_ALL_ALIVE_BLOBS(cb) { 330 f->do_code_blob(cb); 331 332 #ifdef ASSERT 333 if (cb->is_nmethod()) 334 ((nmethod*)cb)->verify_scavenge_root_oops(); 335 #endif //ASSERT 336 } 337 } 338 339 // Walk the list of methods which might contain non-perm oops. 340 void CodeCache::scavenge_root_nmethods_do(CodeBlobClosure* f) { 341 assert_locked_or_safepoint(CodeCache_lock); 342 343 if (UseG1GC) { 344 return; 345 } 346 347 debug_only(mark_scavenge_root_nmethods()); 348 349 for (nmethod* cur = scavenge_root_nmethods(); cur != NULL; cur = cur->scavenge_root_link()) { 350 debug_only(cur->clear_scavenge_root_marked()); 351 assert(cur->scavenge_root_not_marked(), ""); 352 assert(cur->on_scavenge_root_list(), "else shouldn't be on this list"); 353 354 bool is_live = (!cur->is_zombie() && !cur->is_unloaded()); 355 #ifndef PRODUCT 356 if (TraceScavenge) { 357 cur->print_on(tty, is_live ? "scavenge root" : "dead scavenge root"); tty->cr(); 358 } 359 #endif //PRODUCT 360 if (is_live) { 361 // Perform cur->oops_do(f), maybe just once per nmethod. 362 f->do_code_blob(cur); 363 } 364 } 365 366 // Check for stray marks. 367 debug_only(verify_perm_nmethods(NULL)); 368 } 369 370 void CodeCache::add_scavenge_root_nmethod(nmethod* nm) { 371 assert_locked_or_safepoint(CodeCache_lock); 372 373 if (UseG1GC) { 374 return; 375 } 376 377 nm->set_on_scavenge_root_list(); 378 nm->set_scavenge_root_link(_scavenge_root_nmethods); 379 set_scavenge_root_nmethods(nm); 380 print_trace("add_scavenge_root", nm); 381 } 382 383 void CodeCache::drop_scavenge_root_nmethod(nmethod* nm) { 384 assert_locked_or_safepoint(CodeCache_lock); 385 386 if (UseG1GC) { 387 return; 388 } 389 390 print_trace("drop_scavenge_root", nm); 391 nmethod* last = NULL; 392 nmethod* cur = scavenge_root_nmethods(); 393 while (cur != NULL) { 394 nmethod* next = cur->scavenge_root_link(); 395 if (cur == nm) { 396 if (last != NULL) 397 last->set_scavenge_root_link(next); 398 else set_scavenge_root_nmethods(next); 399 nm->set_scavenge_root_link(NULL); 400 nm->clear_on_scavenge_root_list(); 401 return; 402 } 403 last = cur; 404 cur = next; 405 } 406 assert(false, "should have been on list"); 407 } 408 409 void CodeCache::prune_scavenge_root_nmethods() { 410 assert_locked_or_safepoint(CodeCache_lock); 411 412 if (UseG1GC) { 413 return; 414 } 415 416 debug_only(mark_scavenge_root_nmethods()); 417 418 nmethod* last = NULL; 419 nmethod* cur = scavenge_root_nmethods(); 420 while (cur != NULL) { 421 nmethod* next = cur->scavenge_root_link(); 422 debug_only(cur->clear_scavenge_root_marked()); 423 assert(cur->scavenge_root_not_marked(), ""); 424 assert(cur->on_scavenge_root_list(), "else shouldn't be on this list"); 425 426 if (!cur->is_zombie() && !cur->is_unloaded() 427 && cur->detect_scavenge_root_oops()) { 428 // Keep it. Advance 'last' to prevent deletion. 429 last = cur; 430 } else { 431 // Prune it from the list, so we don't have to look at it any more. 432 print_trace("prune_scavenge_root", cur); 433 cur->set_scavenge_root_link(NULL); 434 cur->clear_on_scavenge_root_list(); 435 if (last != NULL) 436 last->set_scavenge_root_link(next); 437 else set_scavenge_root_nmethods(next); 438 } 439 cur = next; 440 } 441 442 // Check for stray marks. 443 debug_only(verify_perm_nmethods(NULL)); 444 } 445 446 #ifndef PRODUCT 447 void CodeCache::asserted_non_scavengable_nmethods_do(CodeBlobClosure* f) { 448 if (UseG1GC) { 449 return; 450 } 451 452 // While we are here, verify the integrity of the list. 453 mark_scavenge_root_nmethods(); 454 for (nmethod* cur = scavenge_root_nmethods(); cur != NULL; cur = cur->scavenge_root_link()) { 455 assert(cur->on_scavenge_root_list(), "else shouldn't be on this list"); 456 cur->clear_scavenge_root_marked(); 457 } 458 verify_perm_nmethods(f); 459 } 460 461 // Temporarily mark nmethods that are claimed to be on the non-perm list. 462 void CodeCache::mark_scavenge_root_nmethods() { 463 FOR_ALL_ALIVE_BLOBS(cb) { 464 if (cb->is_nmethod()) { 465 nmethod *nm = (nmethod*)cb; 466 assert(nm->scavenge_root_not_marked(), "clean state"); 467 if (nm->on_scavenge_root_list()) 468 nm->set_scavenge_root_marked(); 469 } 470 } 471 } 472 473 // If the closure is given, run it on the unlisted nmethods. 474 // Also make sure that the effects of mark_scavenge_root_nmethods is gone. 475 void CodeCache::verify_perm_nmethods(CodeBlobClosure* f_or_null) { 476 FOR_ALL_ALIVE_BLOBS(cb) { 477 bool call_f = (f_or_null != NULL); 478 if (cb->is_nmethod()) { 479 nmethod *nm = (nmethod*)cb; 480 assert(nm->scavenge_root_not_marked(), "must be already processed"); 481 if (nm->on_scavenge_root_list()) 482 call_f = false; // don't show this one to the client 483 nm->verify_scavenge_root_oops(); 484 } else { 485 call_f = false; // not an nmethod 486 } 487 if (call_f) f_or_null->do_code_blob(cb); 488 } 489 } 490 #endif //PRODUCT 491 492 void CodeCache::verify_clean_inline_caches() { 493 #ifdef ASSERT 494 FOR_ALL_ALIVE_BLOBS(cb) { 495 if (cb->is_nmethod()) { 496 nmethod* nm = (nmethod*)cb; 497 assert(!nm->is_unloaded(), "Tautology"); 498 nm->verify_clean_inline_caches(); 499 nm->verify(); 500 } 501 } 502 #endif 503 } 504 505 void CodeCache::verify_icholder_relocations() { 506 #ifdef ASSERT 507 // make sure that we aren't leaking icholders 508 int count = 0; 509 FOR_ALL_BLOBS(cb) { 510 if (cb->is_nmethod()) { 511 nmethod* nm = (nmethod*)cb; 512 count += nm->verify_icholder_relocations(); 513 } 514 } 515 516 assert(count + InlineCacheBuffer::pending_icholder_count() + CompiledICHolder::live_not_claimed_count() == 517 CompiledICHolder::live_count(), "must agree"); 518 #endif 519 } 520 521 void CodeCache::gc_prologue() { 522 } 523 524 void CodeCache::gc_epilogue() { 525 assert_locked_or_safepoint(CodeCache_lock); 526 NOT_DEBUG(if (needs_cache_clean())) { 527 FOR_ALL_ALIVE_BLOBS(cb) { 528 if (cb->is_nmethod()) { 529 nmethod *nm = (nmethod*)cb; 530 assert(!nm->is_unloaded(), "Tautology"); 531 DEBUG_ONLY(if (needs_cache_clean())) { 532 nm->cleanup_inline_caches(); 533 } 534 DEBUG_ONLY(nm->verify()); 535 DEBUG_ONLY(nm->verify_oop_relocations()); 536 } 537 } 538 } 539 set_needs_cache_clean(false); 540 prune_scavenge_root_nmethods(); 541 542 verify_icholder_relocations(); 543 } 544 545 void CodeCache::verify_oops() { 546 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 547 VerifyOopClosure voc; 548 FOR_ALL_ALIVE_BLOBS(cb) { 549 if (cb->is_nmethod()) { 550 nmethod *nm = (nmethod*)cb; 551 nm->oops_do(&voc); 552 nm->verify_oop_relocations(); 553 } 554 } 555 } 556 557 558 address CodeCache::first_address() { 559 assert_locked_or_safepoint(CodeCache_lock); 560 return (address)_heap->low_boundary(); 561 } 562 563 564 address CodeCache::last_address() { 565 assert_locked_or_safepoint(CodeCache_lock); 566 return (address)_heap->high(); 567 } 568 569 /** 570 * Returns the reverse free ratio. E.g., if 25% (1/4) of the code cache 571 * is free, reverse_free_ratio() returns 4. 572 */ 573 double CodeCache::reverse_free_ratio() { 574 double unallocated_capacity = (double)(CodeCache::unallocated_capacity() - CodeCacheMinimumFreeSpace); 575 double max_capacity = (double)CodeCache::max_capacity(); 576 return max_capacity / unallocated_capacity; 577 } 578 579 void icache_init(); 580 581 void CodeCache::initialize() { 582 assert(CodeCacheSegmentSize >= (uintx)CodeEntryAlignment, "CodeCacheSegmentSize must be large enough to align entry points"); 583 #ifdef COMPILER2 584 assert(CodeCacheSegmentSize >= (uintx)OptoLoopAlignment, "CodeCacheSegmentSize must be large enough to align inner loops"); 585 #endif 586 assert(CodeCacheSegmentSize >= sizeof(jdouble), "CodeCacheSegmentSize must be large enough to align constants"); 587 // This was originally just a check of the alignment, causing failure, instead, round 588 // the code cache to the page size. In particular, Solaris is moving to a larger 589 // default page size. 590 CodeCacheExpansionSize = round_to(CodeCacheExpansionSize, os::vm_page_size()); 591 InitialCodeCacheSize = round_to(InitialCodeCacheSize, os::vm_page_size()); 592 ReservedCodeCacheSize = round_to(ReservedCodeCacheSize, os::vm_page_size()); 593 if (!_heap->reserve(ReservedCodeCacheSize, InitialCodeCacheSize, CodeCacheSegmentSize)) { 594 vm_exit_during_initialization("Could not reserve enough space for code cache"); 595 } 596 597 MemoryService::add_code_heap_memory_pool(_heap); 598 599 // Initialize ICache flush mechanism 600 // This service is needed for os::register_code_area 601 icache_init(); 602 603 // Give OS a chance to register generated code area. 604 // This is used on Windows 64 bit platforms to register 605 // Structured Exception Handlers for our generated code. 606 os::register_code_area(_heap->low_boundary(), _heap->high_boundary()); 607 } 608 609 610 void codeCache_init() { 611 CodeCache::initialize(); 612 } 613 614 //------------------------------------------------------------------------------------------------ 615 616 int CodeCache::number_of_nmethods_with_dependencies() { 617 return _number_of_nmethods_with_dependencies; 618 } 619 620 void CodeCache::clear_inline_caches() { 621 assert_locked_or_safepoint(CodeCache_lock); 622 FOR_ALL_ALIVE_NMETHODS(nm) { 623 nm->clear_inline_caches(); 624 } 625 } 626 627 #ifndef PRODUCT 628 // used to keep track of how much time is spent in mark_for_deoptimization 629 static elapsedTimer dependentCheckTime; 630 static int dependentCheckCount = 0; 631 #endif // PRODUCT 632 633 634 int CodeCache::mark_for_deoptimization(DepChange& changes) { 635 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 636 637 #ifndef PRODUCT 638 dependentCheckTime.start(); 639 dependentCheckCount++; 640 #endif // PRODUCT 641 642 int number_of_marked_CodeBlobs = 0; 643 644 // search the hierarchy looking for nmethods which are affected by the loading of this class 645 646 // then search the interfaces this class implements looking for nmethods 647 // which might be dependent of the fact that an interface only had one 648 // implementor. 649 650 { No_Safepoint_Verifier nsv; 651 for (DepChange::ContextStream str(changes, nsv); str.next(); ) { 652 Klass* d = str.klass(); 653 number_of_marked_CodeBlobs += InstanceKlass::cast(d)->mark_dependent_nmethods(changes); 654 } 655 } 656 657 if (VerifyDependencies) { 658 // Turn off dependency tracing while actually testing deps. 659 NOT_PRODUCT( FlagSetting fs(TraceDependencies, false) ); 660 FOR_ALL_ALIVE_NMETHODS(nm) { 661 if (!nm->is_marked_for_deoptimization() && 662 nm->check_all_dependencies()) { 663 ResourceMark rm; 664 tty->print_cr("Should have been marked for deoptimization:"); 665 changes.print(); 666 nm->print(); 667 nm->print_dependencies(); 668 } 669 } 670 } 671 672 #ifndef PRODUCT 673 dependentCheckTime.stop(); 674 #endif // PRODUCT 675 676 return number_of_marked_CodeBlobs; 677 } 678 679 680 #ifdef HOTSWAP 681 int CodeCache::mark_for_evol_deoptimization(instanceKlassHandle dependee) { 682 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 683 int number_of_marked_CodeBlobs = 0; 684 685 // Deoptimize all methods of the evolving class itself 686 Array<Method*>* old_methods = dependee->methods(); 687 for (int i = 0; i < old_methods->length(); i++) { 688 ResourceMark rm; 689 Method* old_method = old_methods->at(i); 690 nmethod *nm = old_method->code(); 691 if (nm != NULL) { 692 nm->mark_for_deoptimization(); 693 number_of_marked_CodeBlobs++; 694 } 695 } 696 697 FOR_ALL_ALIVE_NMETHODS(nm) { 698 if (nm->is_marked_for_deoptimization()) { 699 // ...Already marked in the previous pass; don't count it again. 700 } else if (nm->is_evol_dependent_on(dependee())) { 701 ResourceMark rm; 702 nm->mark_for_deoptimization(); 703 number_of_marked_CodeBlobs++; 704 } else { 705 // flush caches in case they refer to a redefined Method* 706 nm->clear_inline_caches(); 707 } 708 } 709 710 return number_of_marked_CodeBlobs; 711 } 712 #endif // HOTSWAP 713 714 715 // Deoptimize all methods 716 void CodeCache::mark_all_nmethods_for_deoptimization() { 717 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 718 FOR_ALL_ALIVE_NMETHODS(nm) { 719 if (!nm->method()->is_method_handle_intrinsic()) { 720 nm->mark_for_deoptimization(); 721 } 722 } 723 } 724 725 726 int CodeCache::mark_for_deoptimization(Method* dependee) { 727 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 728 int number_of_marked_CodeBlobs = 0; 729 730 FOR_ALL_ALIVE_NMETHODS(nm) { 731 if (nm->is_dependent_on_method(dependee)) { 732 ResourceMark rm; 733 nm->mark_for_deoptimization(); 734 number_of_marked_CodeBlobs++; 735 } 736 } 737 738 return number_of_marked_CodeBlobs; 739 } 740 741 void CodeCache::make_marked_nmethods_not_entrant() { 742 assert_locked_or_safepoint(CodeCache_lock); 743 FOR_ALL_ALIVE_NMETHODS(nm) { 744 if (nm->is_marked_for_deoptimization()) { 745 nm->make_not_entrant(); 746 } 747 } 748 } 749 750 void CodeCache::verify() { 751 _heap->verify(); 752 FOR_ALL_ALIVE_BLOBS(p) { 753 p->verify(); 754 } 755 } 756 757 void CodeCache::report_codemem_full() { 758 _codemem_full_count++; 759 EventCodeCacheFull event; 760 if (event.should_commit()) { 761 event.set_startAddress((u8)low_bound()); 762 event.set_commitedTopAddress((u8)high()); 763 event.set_reservedTopAddress((u8)high_bound()); 764 event.set_entryCount(nof_blobs()); 765 event.set_methodCount(nof_nmethods()); 766 event.set_adaptorCount(nof_adapters()); 767 event.set_unallocatedCapacity(unallocated_capacity()/K); 768 event.set_fullCount(_codemem_full_count); 769 event.commit(); 770 } 771 } 772 773 //------------------------------------------------------------------------------------------------ 774 // Non-product version 775 776 #ifndef PRODUCT 777 778 void CodeCache::verify_if_often() { 779 if (VerifyCodeCacheOften) { 780 _heap->verify(); 781 } 782 } 783 784 void CodeCache::print_trace(const char* event, CodeBlob* cb, int size) { 785 if (PrintCodeCache2) { // Need to add a new flag 786 ResourceMark rm; 787 if (size == 0) size = cb->size(); 788 tty->print_cr("CodeCache %s: addr: " INTPTR_FORMAT ", size: 0x%x", event, p2i(cb), size); 789 } 790 } 791 792 void CodeCache::print_internals() { 793 int nmethodCount = 0; 794 int runtimeStubCount = 0; 795 int adapterCount = 0; 796 int deoptimizationStubCount = 0; 797 int uncommonTrapStubCount = 0; 798 int bufferBlobCount = 0; 799 int total = 0; 800 int nmethodAlive = 0; 801 int nmethodNotEntrant = 0; 802 int nmethodZombie = 0; 803 int nmethodUnloaded = 0; 804 int nmethodJava = 0; 805 int nmethodNative = 0; 806 int maxCodeSize = 0; 807 ResourceMark rm; 808 809 CodeBlob *cb; 810 for (cb = first(); cb != NULL; cb = next(cb)) { 811 total++; 812 if (cb->is_nmethod()) { 813 nmethod* nm = (nmethod*)cb; 814 815 if (Verbose && nm->method() != NULL) { 816 ResourceMark rm; 817 char *method_name = nm->method()->name_and_sig_as_C_string(); 818 tty->print("%s", method_name); 819 if(nm->is_alive()) { tty->print_cr(" alive"); } 820 if(nm->is_not_entrant()) { tty->print_cr(" not-entrant"); } 821 if(nm->is_zombie()) { tty->print_cr(" zombie"); } 822 } 823 824 nmethodCount++; 825 826 if(nm->is_alive()) { nmethodAlive++; } 827 if(nm->is_not_entrant()) { nmethodNotEntrant++; } 828 if(nm->is_zombie()) { nmethodZombie++; } 829 if(nm->is_unloaded()) { nmethodUnloaded++; } 830 if(nm->is_native_method()) { nmethodNative++; } 831 832 if(nm->method() != NULL && nm->is_java_method()) { 833 nmethodJava++; 834 if (nm->insts_size() > maxCodeSize) { 835 maxCodeSize = nm->insts_size(); 836 } 837 } 838 } else if (cb->is_runtime_stub()) { 839 runtimeStubCount++; 840 } else if (cb->is_deoptimization_stub()) { 841 deoptimizationStubCount++; 842 } else if (cb->is_uncommon_trap_stub()) { 843 uncommonTrapStubCount++; 844 } else if (cb->is_adapter_blob()) { 845 adapterCount++; 846 } else if (cb->is_buffer_blob()) { 847 bufferBlobCount++; 848 } 849 } 850 851 int bucketSize = 512; 852 int bucketLimit = maxCodeSize / bucketSize + 1; 853 int *buckets = NEW_C_HEAP_ARRAY(int, bucketLimit, mtCode); 854 memset(buckets,0,sizeof(int) * bucketLimit); 855 856 for (cb = first(); cb != NULL; cb = next(cb)) { 857 if (cb->is_nmethod()) { 858 nmethod* nm = (nmethod*)cb; 859 if(nm->is_java_method()) { 860 buckets[nm->insts_size() / bucketSize]++; 861 } 862 } 863 } 864 tty->print_cr("Code Cache Entries (total of %d)",total); 865 tty->print_cr("-------------------------------------------------"); 866 tty->print_cr("nmethods: %d",nmethodCount); 867 tty->print_cr("\talive: %d",nmethodAlive); 868 tty->print_cr("\tnot_entrant: %d",nmethodNotEntrant); 869 tty->print_cr("\tzombie: %d",nmethodZombie); 870 tty->print_cr("\tunloaded: %d",nmethodUnloaded); 871 tty->print_cr("\tjava: %d",nmethodJava); 872 tty->print_cr("\tnative: %d",nmethodNative); 873 tty->print_cr("runtime_stubs: %d",runtimeStubCount); 874 tty->print_cr("adapters: %d",adapterCount); 875 tty->print_cr("buffer blobs: %d",bufferBlobCount); 876 tty->print_cr("deoptimization_stubs: %d",deoptimizationStubCount); 877 tty->print_cr("uncommon_traps: %d",uncommonTrapStubCount); 878 tty->print_cr("\nnmethod size distribution (non-zombie java)"); 879 tty->print_cr("-------------------------------------------------"); 880 881 for(int i=0; i<bucketLimit; i++) { 882 if(buckets[i] != 0) { 883 tty->print("%d - %d bytes",i*bucketSize,(i+1)*bucketSize); 884 tty->fill_to(40); 885 tty->print_cr("%d",buckets[i]); 886 } 887 } 888 889 FREE_C_HEAP_ARRAY(int, buckets, mtCode); 890 } 891 892 #endif // !PRODUCT 893 894 void CodeCache::print() { 895 print_summary(tty); 896 897 #ifndef PRODUCT 898 if (!Verbose) return; 899 900 CodeBlob_sizes live; 901 CodeBlob_sizes dead; 902 903 FOR_ALL_BLOBS(p) { 904 if (!p->is_alive()) { 905 dead.add(p); 906 } else { 907 live.add(p); 908 } 909 } 910 911 tty->print_cr("CodeCache:"); 912 913 tty->print_cr("nmethod dependency checking time %f, per dependent %f", dependentCheckTime.seconds(), 914 dependentCheckTime.seconds() / dependentCheckCount); 915 916 if (!live.is_empty()) { 917 live.print("live"); 918 } 919 if (!dead.is_empty()) { 920 dead.print("dead"); 921 } 922 923 924 if (WizardMode) { 925 // print the oop_map usage 926 int code_size = 0; 927 int number_of_blobs = 0; 928 int number_of_oop_maps = 0; 929 int map_size = 0; 930 FOR_ALL_BLOBS(p) { 931 if (p->is_alive()) { 932 number_of_blobs++; 933 code_size += p->code_size(); 934 OopMapSet* set = p->oop_maps(); 935 if (set != NULL) { 936 number_of_oop_maps += set->size(); 937 map_size += set->heap_size(); 938 } 939 } 940 } 941 tty->print_cr("OopMaps"); 942 tty->print_cr(" #blobs = %d", number_of_blobs); 943 tty->print_cr(" code size = %d", code_size); 944 tty->print_cr(" #oop_maps = %d", number_of_oop_maps); 945 tty->print_cr(" map size = %d", map_size); 946 } 947 948 #endif // !PRODUCT 949 } 950 951 void CodeCache::print_summary(outputStream* st, bool detailed) { 952 size_t total = (_heap->high_boundary() - _heap->low_boundary()); 953 st->print_cr("CodeCache: size=" SIZE_FORMAT "Kb used=" SIZE_FORMAT 954 "Kb max_used=" SIZE_FORMAT "Kb free=" SIZE_FORMAT "Kb", 955 total/K, (total - unallocated_capacity())/K, 956 maxCodeCacheUsed/K, unallocated_capacity()/K); 957 958 if (detailed) { 959 st->print_cr(" bounds [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT "]", 960 p2i(_heap->low_boundary()), 961 p2i(_heap->high()), 962 p2i(_heap->high_boundary())); 963 st->print_cr(" total_blobs=" UINT32_FORMAT " nmethods=" UINT32_FORMAT 964 " adapters=" UINT32_FORMAT, 965 nof_blobs(), nof_nmethods(), nof_adapters()); 966 st->print_cr(" compilation: %s", CompileBroker::should_compile_new_jobs() ? 967 "enabled" : Arguments::mode() == Arguments::_int ? 968 "disabled (interpreter mode)" : 969 "disabled (not enough contiguous free space left)"); 970 } 971 } 972 973 void CodeCache::log_state(outputStream* st) { 974 st->print(" total_blobs='" UINT32_FORMAT "' nmethods='" UINT32_FORMAT "'" 975 " adapters='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'", 976 nof_blobs(), nof_nmethods(), nof_adapters(), 977 unallocated_capacity()); 978 } 979