1 /* 2 * Copyright (c) 1997, 2015, 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/shared/gcLocker.hpp" 35 #include "memory/allocation.inline.hpp" 36 #include "memory/iterator.hpp" 37 #include "memory/resourceArea.hpp" 38 #include "oops/method.hpp" 39 #include "oops/objArrayOop.hpp" 40 #include "oops/oop.inline.hpp" 41 #include "oops/verifyOopClosure.hpp" 42 #include "runtime/arguments.hpp" 43 #include "runtime/compilationPolicy.hpp" 44 #include "runtime/deoptimization.hpp" 45 #include "runtime/handles.inline.hpp" 46 #include "runtime/icache.hpp" 47 #include "runtime/java.hpp" 48 #include "runtime/mutexLocker.hpp" 49 #include "runtime/sweeper.hpp" 50 #include "services/memoryService.hpp" 51 #include "trace/tracing.hpp" 52 #include "utilities/xmlstream.hpp" 53 #ifdef COMPILER1 54 #include "c1/c1_Compilation.hpp" 55 #include "c1/c1_Compiler.hpp" 56 #endif 57 #ifdef COMPILER2 58 #include "opto/c2compiler.hpp" 59 #include "opto/compile.hpp" 60 #include "opto/node.hpp" 61 #endif 62 63 // Helper class for printing in CodeCache 64 class CodeBlob_sizes { 65 private: 66 int count; 67 int total_size; 68 int header_size; 69 int code_size; 70 int stub_size; 71 int relocation_size; 72 int scopes_oop_size; 73 int scopes_metadata_size; 74 int scopes_data_size; 75 int scopes_pcs_size; 76 77 public: 78 CodeBlob_sizes() { 79 count = 0; 80 total_size = 0; 81 header_size = 0; 82 code_size = 0; 83 stub_size = 0; 84 relocation_size = 0; 85 scopes_oop_size = 0; 86 scopes_metadata_size = 0; 87 scopes_data_size = 0; 88 scopes_pcs_size = 0; 89 } 90 91 int total() { return total_size; } 92 bool is_empty() { return count == 0; } 93 94 void print(const char* title) { 95 tty->print_cr(" #%d %s = %dK (hdr %d%%, loc %d%%, code %d%%, stub %d%%, [oops %d%%, metadata %d%%, data %d%%, pcs %d%%])", 96 count, 97 title, 98 (int)(total() / K), 99 header_size * 100 / total_size, 100 relocation_size * 100 / total_size, 101 code_size * 100 / total_size, 102 stub_size * 100 / total_size, 103 scopes_oop_size * 100 / total_size, 104 scopes_metadata_size * 100 / total_size, 105 scopes_data_size * 100 / total_size, 106 scopes_pcs_size * 100 / total_size); 107 } 108 109 void add(CodeBlob* cb) { 110 count++; 111 total_size += cb->size(); 112 header_size += cb->header_size(); 113 relocation_size += cb->relocation_size(); 114 if (cb->is_nmethod()) { 115 nmethod* nm = cb->as_nmethod_or_null(); 116 code_size += nm->insts_size(); 117 stub_size += nm->stub_size(); 118 119 scopes_oop_size += nm->oops_size(); 120 scopes_metadata_size += nm->metadata_size(); 121 scopes_data_size += nm->scopes_data_size(); 122 scopes_pcs_size += nm->scopes_pcs_size(); 123 } else { 124 code_size += cb->code_size(); 125 } 126 } 127 }; 128 129 // Iterate over all CodeHeaps 130 #define FOR_ALL_HEAPS(heap) for (GrowableArrayIterator<CodeHeap*> heap = _heaps->begin(); heap != _heaps->end(); ++heap) 131 // Iterate over all CodeBlobs (cb) on the given CodeHeap 132 #define FOR_ALL_BLOBS(cb, heap) for (CodeBlob* cb = first_blob(heap); cb != NULL; cb = next_blob(heap, cb)) 133 134 address CodeCache::_low_bound = 0; 135 address CodeCache::_high_bound = 0; 136 int CodeCache::_number_of_nmethods_with_dependencies = 0; 137 bool CodeCache::_needs_cache_clean = false; 138 nmethod* CodeCache::_scavenge_root_nmethods = NULL; 139 140 // Initialize array of CodeHeaps 141 GrowableArray<CodeHeap*>* CodeCache::_heaps = new(ResourceObj::C_HEAP, mtCode) GrowableArray<CodeHeap*> (CodeBlobType::All, true); 142 143 void CodeCache::check_heap_sizes(size_t non_nmethod_size, size_t profiled_size, size_t non_profiled_size, size_t cache_size, bool all_set) { 144 size_t total_size = non_nmethod_size + profiled_size + non_profiled_size; 145 // Prepare error message 146 const char* error = "Invalid code heap sizes"; 147 err_msg message("NonNMethodCodeHeapSize (%zuK) + ProfiledCodeHeapSize (%zuK) + NonProfiledCodeHeapSize (%zuK) = %zuK", 148 non_nmethod_size/K, profiled_size/K, non_profiled_size/K, total_size/K); 149 150 if (total_size > cache_size) { 151 // Some code heap sizes were explicitly set: total_size must be <= cache_size 152 message.append(" is greater than ReservedCodeCacheSize (%zuK).", cache_size/K); 153 vm_exit_during_initialization(error, message); 154 } else if (all_set && total_size != cache_size) { 155 // All code heap sizes were explicitly set: total_size must equal cache_size 156 message.append(" is not equal to ReservedCodeCacheSize (%zuK).", cache_size/K); 157 vm_exit_during_initialization(error, message); 158 } 159 } 160 161 void CodeCache::initialize_heaps() { 162 bool non_nmethod_set = FLAG_IS_CMDLINE(NonNMethodCodeHeapSize); 163 bool profiled_set = FLAG_IS_CMDLINE(ProfiledCodeHeapSize); 164 bool non_profiled_set = FLAG_IS_CMDLINE(NonProfiledCodeHeapSize); 165 size_t min_size = os::vm_page_size(); 166 size_t cache_size = ReservedCodeCacheSize; 167 size_t non_nmethod_size = NonNMethodCodeHeapSize; 168 size_t profiled_size = ProfiledCodeHeapSize; 169 size_t non_profiled_size = NonProfiledCodeHeapSize; 170 // Check if total size set via command line flags exceeds the reserved size 171 check_heap_sizes((non_nmethod_set ? non_nmethod_size : min_size), 172 (profiled_set ? profiled_size : min_size), 173 (non_profiled_set ? non_profiled_size : min_size), 174 cache_size, 175 non_nmethod_set && profiled_set && non_profiled_set); 176 177 // Determine size of compiler buffers 178 size_t code_buffers_size = 0; 179 #ifdef COMPILER1 180 // C1 temporary code buffers (see Compiler::init_buffer_blob()) 181 const int c1_count = CompilationPolicy::policy()->compiler_count(CompLevel_simple); 182 code_buffers_size += c1_count * Compiler::code_buffer_size(); 183 #endif 184 #ifdef COMPILER2 185 // C2 scratch buffers (see Compile::init_scratch_buffer_blob()) 186 const int c2_count = CompilationPolicy::policy()->compiler_count(CompLevel_full_optimization); 187 // Initial size of constant table (this may be increased if a compiled method needs more space) 188 code_buffers_size += c2_count * C2Compiler::initial_code_buffer_size(); 189 #endif 190 191 // Increase default non_nmethod_size to account for compiler buffers 192 if (!non_nmethod_set) { 193 non_nmethod_size += code_buffers_size; 194 } 195 // Calculate default CodeHeap sizes if not set by user 196 if (!non_nmethod_set && !profiled_set && !non_profiled_set) { 197 // Check if we have enough space for the non-nmethod code heap 198 if (cache_size > non_nmethod_size) { 199 // Use the default value for non_nmethod_size and one half of the 200 // remaining size for non-profiled and one half for profiled methods 201 size_t remaining_size = cache_size - non_nmethod_size; 202 profiled_size = remaining_size / 2; 203 non_profiled_size = remaining_size - profiled_size; 204 } else { 205 // Use all space for the non-nmethod heap and set other heaps to minimal size 206 non_nmethod_size = cache_size - 2 * min_size; 207 profiled_size = min_size; 208 non_profiled_size = min_size; 209 } 210 } else if (!non_nmethod_set || !profiled_set || !non_profiled_set) { 211 // The user explicitly set some code heap sizes. Increase or decrease the (default) 212 // sizes of the other code heaps accordingly. First adapt non-profiled and profiled 213 // code heap sizes and then only change non-nmethod code heap size if still necessary. 214 intx diff_size = cache_size - (non_nmethod_size + profiled_size + non_profiled_size); 215 if (non_profiled_set) { 216 if (!profiled_set) { 217 // Adapt size of profiled code heap 218 if (diff_size < 0 && ((intx)profiled_size + diff_size) <= 0) { 219 // Not enough space available, set to minimum size 220 diff_size += profiled_size - min_size; 221 profiled_size = min_size; 222 } else { 223 profiled_size += diff_size; 224 diff_size = 0; 225 } 226 } 227 } else if (profiled_set) { 228 // Adapt size of non-profiled code heap 229 if (diff_size < 0 && ((intx)non_profiled_size + diff_size) <= 0) { 230 // Not enough space available, set to minimum size 231 diff_size += non_profiled_size - min_size; 232 non_profiled_size = min_size; 233 } else { 234 non_profiled_size += diff_size; 235 diff_size = 0; 236 } 237 } else if (non_nmethod_set) { 238 // Distribute remaining size between profiled and non-profiled code heaps 239 diff_size = cache_size - non_nmethod_size; 240 profiled_size = diff_size / 2; 241 non_profiled_size = diff_size - profiled_size; 242 diff_size = 0; 243 } 244 if (diff_size != 0) { 245 // Use non-nmethod code heap for remaining space requirements 246 assert(!non_nmethod_set && ((intx)non_nmethod_size + diff_size) > 0, "sanity"); 247 non_nmethod_size += diff_size; 248 } 249 } 250 251 // We do not need the profiled CodeHeap, use all space for the non-profiled CodeHeap 252 if(!heap_available(CodeBlobType::MethodProfiled)) { 253 non_profiled_size += profiled_size; 254 profiled_size = 0; 255 } 256 // We do not need the non-profiled CodeHeap, use all space for the non-nmethod CodeHeap 257 if(!heap_available(CodeBlobType::MethodNonProfiled)) { 258 non_nmethod_size += non_profiled_size; 259 non_profiled_size = 0; 260 } 261 // Make sure we have enough space for VM internal code 262 uint min_code_cache_size = CodeCacheMinimumUseSpace DEBUG_ONLY(* 3); 263 if (non_nmethod_size < (min_code_cache_size + code_buffers_size)) { 264 vm_exit_during_initialization(err_msg( 265 "Not enough space in non-nmethod code heap to run VM: %zuK < %zuK", 266 non_nmethod_size/K, (min_code_cache_size + code_buffers_size)/K)); 267 } 268 269 // Verify sizes and update flag values 270 assert(non_profiled_size + profiled_size + non_nmethod_size == cache_size, "Invalid code heap sizes"); 271 FLAG_SET_ERGO(uintx, NonNMethodCodeHeapSize, non_nmethod_size); 272 FLAG_SET_ERGO(uintx, ProfiledCodeHeapSize, profiled_size); 273 FLAG_SET_ERGO(uintx, NonProfiledCodeHeapSize, non_profiled_size); 274 275 // Align CodeHeaps 276 size_t alignment = heap_alignment(); 277 non_nmethod_size = align_size_up(non_nmethod_size, alignment); 278 profiled_size = align_size_down(profiled_size, alignment); 279 280 // Reserve one continuous chunk of memory for CodeHeaps and split it into 281 // parts for the individual heaps. The memory layout looks like this: 282 // ---------- high ----------- 283 // Non-profiled nmethods 284 // Profiled nmethods 285 // Non-nmethods 286 // ---------- low ------------ 287 ReservedCodeSpace rs = reserve_heap_memory(cache_size); 288 ReservedSpace non_method_space = rs.first_part(non_nmethod_size); 289 ReservedSpace rest = rs.last_part(non_nmethod_size); 290 ReservedSpace profiled_space = rest.first_part(profiled_size); 291 ReservedSpace non_profiled_space = rest.last_part(profiled_size); 292 293 // Non-nmethods (stubs, adapters, ...) 294 add_heap(non_method_space, "CodeHeap 'non-nmethods'", CodeBlobType::NonNMethod); 295 // Tier 2 and tier 3 (profiled) methods 296 add_heap(profiled_space, "CodeHeap 'profiled nmethods'", CodeBlobType::MethodProfiled); 297 // Tier 1 and tier 4 (non-profiled) methods and native methods 298 add_heap(non_profiled_space, "CodeHeap 'non-profiled nmethods'", CodeBlobType::MethodNonProfiled); 299 } 300 301 size_t CodeCache::heap_alignment() { 302 // If large page support is enabled, align code heaps according to large 303 // page size to make sure that code cache is covered by large pages. 304 const size_t page_size = os::can_execute_large_page_memory() ? 305 os::page_size_for_region_unaligned(ReservedCodeCacheSize, 8) : 306 os::vm_page_size(); 307 return MAX2(page_size, (size_t) os::vm_allocation_granularity()); 308 } 309 310 ReservedCodeSpace CodeCache::reserve_heap_memory(size_t size) { 311 // Determine alignment 312 const size_t page_size = os::can_execute_large_page_memory() ? 313 MIN2(os::page_size_for_region_aligned(InitialCodeCacheSize, 8), 314 os::page_size_for_region_aligned(size, 8)) : 315 os::vm_page_size(); 316 const size_t granularity = os::vm_allocation_granularity(); 317 const size_t r_align = MAX2(page_size, granularity); 318 const size_t r_size = align_size_up(size, r_align); 319 const size_t rs_align = page_size == (size_t) os::vm_page_size() ? 0 : 320 MAX2(page_size, granularity); 321 322 ReservedCodeSpace rs(r_size, rs_align, rs_align > 0); 323 324 if (!rs.is_reserved()) { 325 vm_exit_during_initialization("Could not reserve enough space for code cache"); 326 } 327 328 // Initialize bounds 329 _low_bound = (address)rs.base(); 330 _high_bound = _low_bound + rs.size(); 331 332 return rs; 333 } 334 335 bool CodeCache::heap_available(int code_blob_type) { 336 if (!SegmentedCodeCache) { 337 // No segmentation: use a single code heap 338 return (code_blob_type == CodeBlobType::All); 339 } else if (Arguments::mode() == Arguments::_int) { 340 // Interpreter only: we don't need any method code heaps 341 return (code_blob_type == CodeBlobType::NonNMethod); 342 } else if (TieredCompilation && (TieredStopAtLevel > CompLevel_simple)) { 343 // Tiered compilation: use all code heaps 344 return (code_blob_type < CodeBlobType::All); 345 } else { 346 // No TieredCompilation: we only need the non-nmethod and non-profiled code heap 347 return (code_blob_type == CodeBlobType::NonNMethod) || 348 (code_blob_type == CodeBlobType::MethodNonProfiled); 349 } 350 } 351 352 const char* CodeCache::get_code_heap_flag_name(int code_blob_type) { 353 switch(code_blob_type) { 354 case CodeBlobType::NonNMethod: 355 return "NonNMethodCodeHeapSize"; 356 break; 357 case CodeBlobType::MethodNonProfiled: 358 return "NonProfiledCodeHeapSize"; 359 break; 360 case CodeBlobType::MethodProfiled: 361 return "ProfiledCodeHeapSize"; 362 break; 363 } 364 ShouldNotReachHere(); 365 return NULL; 366 } 367 368 void CodeCache::add_heap(ReservedSpace rs, const char* name, int code_blob_type) { 369 // Check if heap is needed 370 if (!heap_available(code_blob_type)) { 371 return; 372 } 373 374 // Create CodeHeap 375 CodeHeap* heap = new CodeHeap(name, code_blob_type); 376 _heaps->append(heap); 377 378 // Reserve Space 379 size_t size_initial = MIN2(InitialCodeCacheSize, rs.size()); 380 size_initial = round_to(size_initial, os::vm_page_size()); 381 if (!heap->reserve(rs, size_initial, CodeCacheSegmentSize)) { 382 vm_exit_during_initialization("Could not reserve enough space for code cache"); 383 } 384 385 // Register the CodeHeap 386 MemoryService::add_code_heap_memory_pool(heap, name); 387 } 388 389 CodeHeap* CodeCache::get_code_heap(const CodeBlob* cb) { 390 assert(cb != NULL, "CodeBlob is null"); 391 FOR_ALL_HEAPS(heap) { 392 if ((*heap)->contains(cb)) { 393 return *heap; 394 } 395 } 396 ShouldNotReachHere(); 397 return NULL; 398 } 399 400 CodeHeap* CodeCache::get_code_heap(int code_blob_type) { 401 FOR_ALL_HEAPS(heap) { 402 if ((*heap)->accepts(code_blob_type)) { 403 return *heap; 404 } 405 } 406 return NULL; 407 } 408 409 CodeBlob* CodeCache::first_blob(CodeHeap* heap) { 410 assert_locked_or_safepoint(CodeCache_lock); 411 assert(heap != NULL, "heap is null"); 412 return (CodeBlob*)heap->first(); 413 } 414 415 CodeBlob* CodeCache::first_blob(int code_blob_type) { 416 if (heap_available(code_blob_type)) { 417 return first_blob(get_code_heap(code_blob_type)); 418 } else { 419 return NULL; 420 } 421 } 422 423 CodeBlob* CodeCache::next_blob(CodeHeap* heap, CodeBlob* cb) { 424 assert_locked_or_safepoint(CodeCache_lock); 425 assert(heap != NULL, "heap is null"); 426 return (CodeBlob*)heap->next(cb); 427 } 428 429 CodeBlob* CodeCache::next_blob(CodeBlob* cb) { 430 return next_blob(get_code_heap(cb), cb); 431 } 432 433 /** 434 * Do not seize the CodeCache lock here--if the caller has not 435 * already done so, we are going to lose bigtime, since the code 436 * cache will contain a garbage CodeBlob until the caller can 437 * run the constructor for the CodeBlob subclass he is busy 438 * instantiating. 439 */ 440 CodeBlob* CodeCache::allocate(int size, int code_blob_type, bool strict) { 441 // Possibly wakes up the sweeper thread. 442 NMethodSweeper::notify(code_blob_type); 443 assert_locked_or_safepoint(CodeCache_lock); 444 assert(size > 0, "Code cache allocation request must be > 0 but is %d", size); 445 if (size <= 0) { 446 return NULL; 447 } 448 CodeBlob* cb = NULL; 449 450 // Get CodeHeap for the given CodeBlobType 451 CodeHeap* heap = get_code_heap(code_blob_type); 452 assert(heap != NULL, "heap is null"); 453 454 while (true) { 455 cb = (CodeBlob*)heap->allocate(size); 456 if (cb != NULL) break; 457 if (!heap->expand_by(CodeCacheExpansionSize)) { 458 // Expansion failed 459 if (SegmentedCodeCache && !strict) { 460 // Fallback solution: Try to store code in another code heap. 461 // Note that in the sweeper, we check the reverse_free_ratio of the code heap 462 // and force stack scanning if less than 10% of the code heap are free. 463 int type = code_blob_type; 464 switch (type) { 465 case CodeBlobType::NonNMethod: 466 type = CodeBlobType::MethodNonProfiled; 467 strict = false; // Allow recursive search for other heaps 468 break; 469 case CodeBlobType::MethodProfiled: 470 type = CodeBlobType::MethodNonProfiled; 471 strict = true; 472 break; 473 case CodeBlobType::MethodNonProfiled: 474 type = CodeBlobType::MethodProfiled; 475 strict = true; 476 break; 477 } 478 if (heap_available(type)) { 479 return allocate(size, type, strict); 480 } 481 } 482 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 483 CompileBroker::handle_full_code_cache(code_blob_type); 484 return NULL; 485 } 486 if (PrintCodeCacheExtension) { 487 ResourceMark rm; 488 if (_heaps->length() >= 1) { 489 tty->print("%s", heap->name()); 490 } else { 491 tty->print("CodeCache"); 492 } 493 tty->print_cr(" extended to [" INTPTR_FORMAT ", " INTPTR_FORMAT "] (" SSIZE_FORMAT " bytes)", 494 (intptr_t)heap->low_boundary(), (intptr_t)heap->high(), 495 (address)heap->high() - (address)heap->low_boundary()); 496 } 497 } 498 print_trace("allocation", cb, size); 499 return cb; 500 } 501 502 void CodeCache::free(CodeBlob* cb) { 503 assert_locked_or_safepoint(CodeCache_lock); 504 CodeHeap* heap = get_code_heap(cb); 505 print_trace("free", cb); 506 if (cb->is_nmethod()) { 507 heap->set_nmethod_count(heap->nmethod_count() - 1); 508 if (((nmethod *)cb)->has_dependencies()) { 509 _number_of_nmethods_with_dependencies--; 510 } 511 } 512 if (cb->is_adapter_blob()) { 513 heap->set_adapter_count(heap->adapter_count() - 1); 514 } 515 516 // Get heap for given CodeBlob and deallocate 517 get_code_heap(cb)->deallocate(cb); 518 519 assert(heap->blob_count() >= 0, "sanity check"); 520 } 521 522 void CodeCache::commit(CodeBlob* cb) { 523 // this is called by nmethod::nmethod, which must already own CodeCache_lock 524 assert_locked_or_safepoint(CodeCache_lock); 525 CodeHeap* heap = get_code_heap(cb); 526 if (cb->is_nmethod()) { 527 heap->set_nmethod_count(heap->nmethod_count() + 1); 528 if (((nmethod *)cb)->has_dependencies()) { 529 _number_of_nmethods_with_dependencies++; 530 } 531 } 532 if (cb->is_adapter_blob()) { 533 heap->set_adapter_count(heap->adapter_count() + 1); 534 } 535 536 // flush the hardware I-cache 537 ICache::invalidate_range(cb->content_begin(), cb->content_size()); 538 } 539 540 bool CodeCache::contains(void *p) { 541 // It should be ok to call contains without holding a lock 542 FOR_ALL_HEAPS(heap) { 543 if ((*heap)->contains(p)) { 544 return true; 545 } 546 } 547 return false; 548 } 549 550 // This method is safe to call without holding the CodeCache_lock, as long as a dead CodeBlob is not 551 // looked up (i.e., one that has been marked for deletion). It only depends on the _segmap to contain 552 // valid indices, which it will always do, as long as the CodeBlob is not in the process of being recycled. 553 CodeBlob* CodeCache::find_blob(void* start) { 554 CodeBlob* result = find_blob_unsafe(start); 555 // We could potentially look up non_entrant methods 556 guarantee(result == NULL || !result->is_zombie() || result->is_locked_by_vm() || is_error_reported(), "unsafe access to zombie method"); 557 return result; 558 } 559 560 // Lookup that does not fail if you lookup a zombie method (if you call this, be sure to know 561 // what you are doing) 562 CodeBlob* CodeCache::find_blob_unsafe(void* start) { 563 // NMT can walk the stack before code cache is created 564 if (_heaps == NULL || _heaps->is_empty()) return NULL; 565 566 FOR_ALL_HEAPS(heap) { 567 CodeBlob* result = (CodeBlob*) (*heap)->find_start(start); 568 if (result != NULL && result->blob_contains((address)start)) { 569 return result; 570 } 571 } 572 return NULL; 573 } 574 575 nmethod* CodeCache::find_nmethod(void* start) { 576 CodeBlob* cb = find_blob(start); 577 assert(cb->is_nmethod(), "did not find an nmethod"); 578 return (nmethod*)cb; 579 } 580 581 void CodeCache::blobs_do(void f(CodeBlob* nm)) { 582 assert_locked_or_safepoint(CodeCache_lock); 583 FOR_ALL_HEAPS(heap) { 584 FOR_ALL_BLOBS(cb, *heap) { 585 f(cb); 586 } 587 } 588 } 589 590 void CodeCache::nmethods_do(void f(nmethod* nm)) { 591 assert_locked_or_safepoint(CodeCache_lock); 592 NMethodIterator iter; 593 while(iter.next()) { 594 f(iter.method()); 595 } 596 } 597 598 void CodeCache::alive_nmethods_do(void f(nmethod* nm)) { 599 assert_locked_or_safepoint(CodeCache_lock); 600 NMethodIterator iter; 601 while(iter.next_alive()) { 602 f(iter.method()); 603 } 604 } 605 606 int CodeCache::alignment_unit() { 607 return (int)_heaps->first()->alignment_unit(); 608 } 609 610 int CodeCache::alignment_offset() { 611 return (int)_heaps->first()->alignment_offset(); 612 } 613 614 // Mark nmethods for unloading if they contain otherwise unreachable oops. 615 void CodeCache::do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred) { 616 assert_locked_or_safepoint(CodeCache_lock); 617 NMethodIterator iter; 618 while(iter.next_alive()) { 619 iter.method()->do_unloading(is_alive, unloading_occurred); 620 } 621 } 622 623 void CodeCache::blobs_do(CodeBlobClosure* f) { 624 assert_locked_or_safepoint(CodeCache_lock); 625 FOR_ALL_HEAPS(heap) { 626 FOR_ALL_BLOBS(cb, *heap) { 627 if (cb->is_alive()) { 628 f->do_code_blob(cb); 629 630 #ifdef ASSERT 631 if (cb->is_nmethod()) 632 ((nmethod*)cb)->verify_scavenge_root_oops(); 633 #endif //ASSERT 634 } 635 } 636 } 637 } 638 639 // Walk the list of methods which might contain non-perm oops. 640 void CodeCache::scavenge_root_nmethods_do(CodeBlobClosure* f) { 641 assert_locked_or_safepoint(CodeCache_lock); 642 643 if (UseG1GC) { 644 return; 645 } 646 647 debug_only(mark_scavenge_root_nmethods()); 648 649 for (nmethod* cur = scavenge_root_nmethods(); cur != NULL; cur = cur->scavenge_root_link()) { 650 debug_only(cur->clear_scavenge_root_marked()); 651 assert(cur->scavenge_root_not_marked(), ""); 652 assert(cur->on_scavenge_root_list(), "else shouldn't be on this list"); 653 654 bool is_live = (!cur->is_zombie() && !cur->is_unloaded()); 655 if (TraceScavenge) { 656 cur->print_on(tty, is_live ? "scavenge root" : "dead scavenge root"); tty->cr(); 657 } 658 if (is_live) { 659 // Perform cur->oops_do(f), maybe just once per nmethod. 660 f->do_code_blob(cur); 661 } 662 } 663 664 // Check for stray marks. 665 debug_only(verify_perm_nmethods(NULL)); 666 } 667 668 void CodeCache::add_scavenge_root_nmethod(nmethod* nm) { 669 assert_locked_or_safepoint(CodeCache_lock); 670 671 if (UseG1GC) { 672 return; 673 } 674 675 nm->set_on_scavenge_root_list(); 676 nm->set_scavenge_root_link(_scavenge_root_nmethods); 677 set_scavenge_root_nmethods(nm); 678 print_trace("add_scavenge_root", nm); 679 } 680 681 void CodeCache::drop_scavenge_root_nmethod(nmethod* nm) { 682 assert_locked_or_safepoint(CodeCache_lock); 683 684 if (UseG1GC) { 685 return; 686 } 687 688 print_trace("drop_scavenge_root", nm); 689 nmethod* last = NULL; 690 nmethod* cur = scavenge_root_nmethods(); 691 while (cur != NULL) { 692 nmethod* next = cur->scavenge_root_link(); 693 if (cur == nm) { 694 if (last != NULL) 695 last->set_scavenge_root_link(next); 696 else set_scavenge_root_nmethods(next); 697 nm->set_scavenge_root_link(NULL); 698 nm->clear_on_scavenge_root_list(); 699 return; 700 } 701 last = cur; 702 cur = next; 703 } 704 assert(false, "should have been on list"); 705 } 706 707 void CodeCache::prune_scavenge_root_nmethods() { 708 assert_locked_or_safepoint(CodeCache_lock); 709 710 if (UseG1GC) { 711 return; 712 } 713 714 debug_only(mark_scavenge_root_nmethods()); 715 716 nmethod* last = NULL; 717 nmethod* cur = scavenge_root_nmethods(); 718 while (cur != NULL) { 719 nmethod* next = cur->scavenge_root_link(); 720 debug_only(cur->clear_scavenge_root_marked()); 721 assert(cur->scavenge_root_not_marked(), ""); 722 assert(cur->on_scavenge_root_list(), "else shouldn't be on this list"); 723 724 if (!cur->is_zombie() && !cur->is_unloaded() 725 && cur->detect_scavenge_root_oops()) { 726 // Keep it. Advance 'last' to prevent deletion. 727 last = cur; 728 } else { 729 // Prune it from the list, so we don't have to look at it any more. 730 print_trace("prune_scavenge_root", cur); 731 cur->set_scavenge_root_link(NULL); 732 cur->clear_on_scavenge_root_list(); 733 if (last != NULL) 734 last->set_scavenge_root_link(next); 735 else set_scavenge_root_nmethods(next); 736 } 737 cur = next; 738 } 739 740 // Check for stray marks. 741 debug_only(verify_perm_nmethods(NULL)); 742 } 743 744 #ifndef PRODUCT 745 void CodeCache::asserted_non_scavengable_nmethods_do(CodeBlobClosure* f) { 746 if (UseG1GC) { 747 return; 748 } 749 750 // While we are here, verify the integrity of the list. 751 mark_scavenge_root_nmethods(); 752 for (nmethod* cur = scavenge_root_nmethods(); cur != NULL; cur = cur->scavenge_root_link()) { 753 assert(cur->on_scavenge_root_list(), "else shouldn't be on this list"); 754 cur->clear_scavenge_root_marked(); 755 } 756 verify_perm_nmethods(f); 757 } 758 759 // Temporarily mark nmethods that are claimed to be on the non-perm list. 760 void CodeCache::mark_scavenge_root_nmethods() { 761 NMethodIterator iter; 762 while(iter.next_alive()) { 763 nmethod* nm = iter.method(); 764 assert(nm->scavenge_root_not_marked(), "clean state"); 765 if (nm->on_scavenge_root_list()) 766 nm->set_scavenge_root_marked(); 767 } 768 } 769 770 // If the closure is given, run it on the unlisted nmethods. 771 // Also make sure that the effects of mark_scavenge_root_nmethods is gone. 772 void CodeCache::verify_perm_nmethods(CodeBlobClosure* f_or_null) { 773 NMethodIterator iter; 774 while(iter.next_alive()) { 775 nmethod* nm = iter.method(); 776 bool call_f = (f_or_null != NULL); 777 assert(nm->scavenge_root_not_marked(), "must be already processed"); 778 if (nm->on_scavenge_root_list()) 779 call_f = false; // don't show this one to the client 780 nm->verify_scavenge_root_oops(); 781 if (call_f) f_or_null->do_code_blob(nm); 782 } 783 } 784 #endif //PRODUCT 785 786 void CodeCache::verify_clean_inline_caches() { 787 #ifdef ASSERT 788 NMethodIterator iter; 789 while(iter.next_alive()) { 790 nmethod* nm = iter.method(); 791 assert(!nm->is_unloaded(), "Tautology"); 792 nm->verify_clean_inline_caches(); 793 nm->verify(); 794 } 795 #endif 796 } 797 798 void CodeCache::verify_icholder_relocations() { 799 #ifdef ASSERT 800 // make sure that we aren't leaking icholders 801 int count = 0; 802 FOR_ALL_HEAPS(heap) { 803 FOR_ALL_BLOBS(cb, *heap) { 804 if (cb->is_nmethod()) { 805 nmethod* nm = (nmethod*)cb; 806 count += nm->verify_icholder_relocations(); 807 } 808 } 809 } 810 811 assert(count + InlineCacheBuffer::pending_icholder_count() + CompiledICHolder::live_not_claimed_count() == 812 CompiledICHolder::live_count(), "must agree"); 813 #endif 814 } 815 816 void CodeCache::gc_prologue() { 817 } 818 819 void CodeCache::gc_epilogue() { 820 assert_locked_or_safepoint(CodeCache_lock); 821 NOT_DEBUG(if (needs_cache_clean())) { 822 NMethodIterator iter; 823 while(iter.next_alive()) { 824 nmethod* nm = iter.method(); 825 assert(!nm->is_unloaded(), "Tautology"); 826 DEBUG_ONLY(if (needs_cache_clean())) { 827 nm->cleanup_inline_caches(); 828 } 829 DEBUG_ONLY(nm->verify()); 830 DEBUG_ONLY(nm->verify_oop_relocations()); 831 } 832 } 833 set_needs_cache_clean(false); 834 prune_scavenge_root_nmethods(); 835 836 verify_icholder_relocations(); 837 } 838 839 void CodeCache::verify_oops() { 840 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 841 VerifyOopClosure voc; 842 NMethodIterator iter; 843 while(iter.next_alive()) { 844 nmethod* nm = iter.method(); 845 nm->oops_do(&voc); 846 nm->verify_oop_relocations(); 847 } 848 } 849 850 int CodeCache::blob_count(int code_blob_type) { 851 CodeHeap* heap = get_code_heap(code_blob_type); 852 return (heap != NULL) ? heap->blob_count() : 0; 853 } 854 855 int CodeCache::blob_count() { 856 int count = 0; 857 FOR_ALL_HEAPS(heap) { 858 count += (*heap)->blob_count(); 859 } 860 return count; 861 } 862 863 int CodeCache::nmethod_count(int code_blob_type) { 864 CodeHeap* heap = get_code_heap(code_blob_type); 865 return (heap != NULL) ? heap->nmethod_count() : 0; 866 } 867 868 int CodeCache::nmethod_count() { 869 int count = 0; 870 FOR_ALL_HEAPS(heap) { 871 count += (*heap)->nmethod_count(); 872 } 873 return count; 874 } 875 876 int CodeCache::adapter_count(int code_blob_type) { 877 CodeHeap* heap = get_code_heap(code_blob_type); 878 return (heap != NULL) ? heap->adapter_count() : 0; 879 } 880 881 int CodeCache::adapter_count() { 882 int count = 0; 883 FOR_ALL_HEAPS(heap) { 884 count += (*heap)->adapter_count(); 885 } 886 return count; 887 } 888 889 address CodeCache::low_bound(int code_blob_type) { 890 CodeHeap* heap = get_code_heap(code_blob_type); 891 return (heap != NULL) ? (address)heap->low_boundary() : NULL; 892 } 893 894 address CodeCache::high_bound(int code_blob_type) { 895 CodeHeap* heap = get_code_heap(code_blob_type); 896 return (heap != NULL) ? (address)heap->high_boundary() : NULL; 897 } 898 899 size_t CodeCache::capacity() { 900 size_t cap = 0; 901 FOR_ALL_HEAPS(heap) { 902 cap += (*heap)->capacity(); 903 } 904 return cap; 905 } 906 907 size_t CodeCache::unallocated_capacity(int code_blob_type) { 908 CodeHeap* heap = get_code_heap(code_blob_type); 909 return (heap != NULL) ? heap->unallocated_capacity() : 0; 910 } 911 912 size_t CodeCache::unallocated_capacity() { 913 size_t unallocated_cap = 0; 914 FOR_ALL_HEAPS(heap) { 915 unallocated_cap += (*heap)->unallocated_capacity(); 916 } 917 return unallocated_cap; 918 } 919 920 size_t CodeCache::max_capacity() { 921 size_t max_cap = 0; 922 FOR_ALL_HEAPS(heap) { 923 max_cap += (*heap)->max_capacity(); 924 } 925 return max_cap; 926 } 927 928 /** 929 * Returns the reverse free ratio. E.g., if 25% (1/4) of the code heap 930 * is free, reverse_free_ratio() returns 4. 931 */ 932 double CodeCache::reverse_free_ratio(int code_blob_type) { 933 CodeHeap* heap = get_code_heap(code_blob_type); 934 if (heap == NULL) { 935 return 0; 936 } 937 938 double unallocated_capacity = MAX2((double)heap->unallocated_capacity(), 1.0); // Avoid division by 0; 939 double max_capacity = (double)heap->max_capacity(); 940 double result = max_capacity / unallocated_capacity; 941 assert (max_capacity >= unallocated_capacity, "Must be"); 942 assert (result >= 1.0, "reverse_free_ratio must be at least 1. It is %f", result); 943 return result; 944 } 945 946 size_t CodeCache::bytes_allocated_in_freelists() { 947 size_t allocated_bytes = 0; 948 FOR_ALL_HEAPS(heap) { 949 allocated_bytes += (*heap)->allocated_in_freelist(); 950 } 951 return allocated_bytes; 952 } 953 954 int CodeCache::allocated_segments() { 955 int number_of_segments = 0; 956 FOR_ALL_HEAPS(heap) { 957 number_of_segments += (*heap)->allocated_segments(); 958 } 959 return number_of_segments; 960 } 961 962 size_t CodeCache::freelists_length() { 963 size_t length = 0; 964 FOR_ALL_HEAPS(heap) { 965 length += (*heap)->freelist_length(); 966 } 967 return length; 968 } 969 970 void icache_init(); 971 972 void CodeCache::initialize() { 973 assert(CodeCacheSegmentSize >= (uintx)CodeEntryAlignment, "CodeCacheSegmentSize must be large enough to align entry points"); 974 #ifdef COMPILER2 975 assert(CodeCacheSegmentSize >= (uintx)OptoLoopAlignment, "CodeCacheSegmentSize must be large enough to align inner loops"); 976 #endif 977 assert(CodeCacheSegmentSize >= sizeof(jdouble), "CodeCacheSegmentSize must be large enough to align constants"); 978 // This was originally just a check of the alignment, causing failure, instead, round 979 // the code cache to the page size. In particular, Solaris is moving to a larger 980 // default page size. 981 CodeCacheExpansionSize = round_to(CodeCacheExpansionSize, os::vm_page_size()); 982 983 if (SegmentedCodeCache) { 984 // Use multiple code heaps 985 initialize_heaps(); 986 } else { 987 // Use a single code heap 988 FLAG_SET_ERGO(uintx, NonNMethodCodeHeapSize, 0); 989 FLAG_SET_ERGO(uintx, ProfiledCodeHeapSize, 0); 990 FLAG_SET_ERGO(uintx, NonProfiledCodeHeapSize, 0); 991 ReservedCodeSpace rs = reserve_heap_memory(ReservedCodeCacheSize); 992 add_heap(rs, "CodeCache", CodeBlobType::All); 993 } 994 995 // Initialize ICache flush mechanism 996 // This service is needed for os::register_code_area 997 icache_init(); 998 999 // Give OS a chance to register generated code area. 1000 // This is used on Windows 64 bit platforms to register 1001 // Structured Exception Handlers for our generated code. 1002 os::register_code_area((char*)low_bound(), (char*)high_bound()); 1003 } 1004 1005 void codeCache_init() { 1006 CodeCache::initialize(); 1007 } 1008 1009 //------------------------------------------------------------------------------------------------ 1010 1011 int CodeCache::number_of_nmethods_with_dependencies() { 1012 return _number_of_nmethods_with_dependencies; 1013 } 1014 1015 void CodeCache::clear_inline_caches() { 1016 assert_locked_or_safepoint(CodeCache_lock); 1017 NMethodIterator iter; 1018 while(iter.next_alive()) { 1019 iter.method()->clear_inline_caches(); 1020 } 1021 } 1022 1023 // Keeps track of time spent for checking dependencies 1024 NOT_PRODUCT(static elapsedTimer dependentCheckTime;) 1025 1026 int CodeCache::mark_for_deoptimization(DepChange& changes) { 1027 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 1028 int number_of_marked_CodeBlobs = 0; 1029 1030 // search the hierarchy looking for nmethods which are affected by the loading of this class 1031 1032 // then search the interfaces this class implements looking for nmethods 1033 // which might be dependent of the fact that an interface only had one 1034 // implementor. 1035 // nmethod::check_all_dependencies works only correctly, if no safepoint 1036 // can happen 1037 No_Safepoint_Verifier nsv; 1038 for (DepChange::ContextStream str(changes, nsv); str.next(); ) { 1039 Klass* d = str.klass(); 1040 number_of_marked_CodeBlobs += InstanceKlass::cast(d)->mark_dependent_nmethods(changes); 1041 } 1042 1043 #ifndef PRODUCT 1044 if (VerifyDependencies) { 1045 // Object pointers are used as unique identifiers for dependency arguments. This 1046 // is only possible if no safepoint, i.e., GC occurs during the verification code. 1047 dependentCheckTime.start(); 1048 nmethod::check_all_dependencies(changes); 1049 dependentCheckTime.stop(); 1050 } 1051 #endif 1052 1053 return number_of_marked_CodeBlobs; 1054 } 1055 1056 1057 #ifdef HOTSWAP 1058 int CodeCache::mark_for_evol_deoptimization(instanceKlassHandle dependee) { 1059 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 1060 int number_of_marked_CodeBlobs = 0; 1061 1062 // Deoptimize all methods of the evolving class itself 1063 Array<Method*>* old_methods = dependee->methods(); 1064 for (int i = 0; i < old_methods->length(); i++) { 1065 ResourceMark rm; 1066 Method* old_method = old_methods->at(i); 1067 nmethod *nm = old_method->code(); 1068 if (nm != NULL) { 1069 nm->mark_for_deoptimization(); 1070 number_of_marked_CodeBlobs++; 1071 } 1072 } 1073 1074 NMethodIterator iter; 1075 while(iter.next_alive()) { 1076 nmethod* nm = iter.method(); 1077 if (nm->is_marked_for_deoptimization()) { 1078 // ...Already marked in the previous pass; don't count it again. 1079 } else if (nm->is_evol_dependent_on(dependee())) { 1080 ResourceMark rm; 1081 nm->mark_for_deoptimization(); 1082 number_of_marked_CodeBlobs++; 1083 } else { 1084 // flush caches in case they refer to a redefined Method* 1085 nm->clear_inline_caches(); 1086 } 1087 } 1088 1089 return number_of_marked_CodeBlobs; 1090 } 1091 #endif // HOTSWAP 1092 1093 1094 // Deoptimize all methods 1095 void CodeCache::mark_all_nmethods_for_deoptimization() { 1096 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 1097 NMethodIterator iter; 1098 while(iter.next_alive()) { 1099 nmethod* nm = iter.method(); 1100 if (!nm->method()->is_method_handle_intrinsic()) { 1101 nm->mark_for_deoptimization(); 1102 } 1103 } 1104 } 1105 1106 int CodeCache::mark_for_deoptimization(Method* dependee) { 1107 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 1108 int number_of_marked_CodeBlobs = 0; 1109 1110 NMethodIterator iter; 1111 while(iter.next_alive()) { 1112 nmethod* nm = iter.method(); 1113 if (nm->is_dependent_on_method(dependee)) { 1114 ResourceMark rm; 1115 nm->mark_for_deoptimization(); 1116 number_of_marked_CodeBlobs++; 1117 } 1118 } 1119 1120 return number_of_marked_CodeBlobs; 1121 } 1122 1123 void CodeCache::make_marked_nmethods_not_entrant() { 1124 assert_locked_or_safepoint(CodeCache_lock); 1125 NMethodIterator iter; 1126 while(iter.next_alive()) { 1127 nmethod* nm = iter.method(); 1128 if (nm->is_marked_for_deoptimization()) { 1129 nm->make_not_entrant(); 1130 } 1131 } 1132 } 1133 1134 // Flushes compiled methods dependent on dependee. 1135 void CodeCache::flush_dependents_on(instanceKlassHandle dependee) { 1136 assert_lock_strong(Compile_lock); 1137 1138 if (number_of_nmethods_with_dependencies() == 0) return; 1139 1140 // CodeCache can only be updated by a thread_in_VM and they will all be 1141 // stopped during the safepoint so CodeCache will be safe to update without 1142 // holding the CodeCache_lock. 1143 1144 KlassDepChange changes(dependee); 1145 1146 // Compute the dependent nmethods 1147 if (mark_for_deoptimization(changes) > 0) { 1148 // At least one nmethod has been marked for deoptimization 1149 VM_Deoptimize op; 1150 VMThread::execute(&op); 1151 } 1152 } 1153 1154 #ifdef HOTSWAP 1155 // Flushes compiled methods dependent on dependee in the evolutionary sense 1156 void CodeCache::flush_evol_dependents_on(instanceKlassHandle ev_k_h) { 1157 // --- Compile_lock is not held. However we are at a safepoint. 1158 assert_locked_or_safepoint(Compile_lock); 1159 if (number_of_nmethods_with_dependencies() == 0) return; 1160 1161 // CodeCache can only be updated by a thread_in_VM and they will all be 1162 // stopped during the safepoint so CodeCache will be safe to update without 1163 // holding the CodeCache_lock. 1164 1165 // Compute the dependent nmethods 1166 if (mark_for_evol_deoptimization(ev_k_h) > 0) { 1167 // At least one nmethod has been marked for deoptimization 1168 1169 // All this already happens inside a VM_Operation, so we'll do all the work here. 1170 // Stuff copied from VM_Deoptimize and modified slightly. 1171 1172 // We do not want any GCs to happen while we are in the middle of this VM operation 1173 ResourceMark rm; 1174 DeoptimizationMarker dm; 1175 1176 // Deoptimize all activations depending on marked nmethods 1177 Deoptimization::deoptimize_dependents(); 1178 1179 // Make the dependent methods not entrant 1180 make_marked_nmethods_not_entrant(); 1181 } 1182 } 1183 #endif // HOTSWAP 1184 1185 1186 // Flushes compiled methods dependent on dependee 1187 void CodeCache::flush_dependents_on_method(methodHandle m_h) { 1188 // --- Compile_lock is not held. However we are at a safepoint. 1189 assert_locked_or_safepoint(Compile_lock); 1190 1191 // CodeCache can only be updated by a thread_in_VM and they will all be 1192 // stopped dring the safepoint so CodeCache will be safe to update without 1193 // holding the CodeCache_lock. 1194 1195 // Compute the dependent nmethods 1196 if (mark_for_deoptimization(m_h()) > 0) { 1197 // At least one nmethod has been marked for deoptimization 1198 1199 // All this already happens inside a VM_Operation, so we'll do all the work here. 1200 // Stuff copied from VM_Deoptimize and modified slightly. 1201 1202 // We do not want any GCs to happen while we are in the middle of this VM operation 1203 ResourceMark rm; 1204 DeoptimizationMarker dm; 1205 1206 // Deoptimize all activations depending on marked nmethods 1207 Deoptimization::deoptimize_dependents(); 1208 1209 // Make the dependent methods not entrant 1210 make_marked_nmethods_not_entrant(); 1211 } 1212 } 1213 1214 void CodeCache::verify() { 1215 assert_locked_or_safepoint(CodeCache_lock); 1216 FOR_ALL_HEAPS(heap) { 1217 (*heap)->verify(); 1218 FOR_ALL_BLOBS(cb, *heap) { 1219 if (cb->is_alive()) { 1220 cb->verify(); 1221 } 1222 } 1223 } 1224 } 1225 1226 // A CodeHeap is full. Print out warning and report event. 1227 void CodeCache::report_codemem_full(int code_blob_type, bool print) { 1228 // Get nmethod heap for the given CodeBlobType and build CodeCacheFull event 1229 CodeHeap* heap = get_code_heap(code_blob_type); 1230 assert(heap != NULL, "heap is null"); 1231 1232 if ((heap->full_count() == 0) || print) { 1233 // Not yet reported for this heap, report 1234 if (SegmentedCodeCache) { 1235 warning("%s is full. Compiler has been disabled.", get_code_heap_name(code_blob_type)); 1236 warning("Try increasing the code heap size using -XX:%s=", get_code_heap_flag_name(code_blob_type)); 1237 } else { 1238 warning("CodeCache is full. Compiler has been disabled."); 1239 warning("Try increasing the code cache size using -XX:ReservedCodeCacheSize="); 1240 } 1241 ResourceMark rm; 1242 stringStream s; 1243 // Dump code cache into a buffer before locking the tty, 1244 { 1245 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 1246 print_summary(&s); 1247 } 1248 ttyLocker ttyl; 1249 tty->print("%s", s.as_string()); 1250 } 1251 1252 heap->report_full(); 1253 1254 EventCodeCacheFull event; 1255 if (event.should_commit()) { 1256 event.set_codeBlobType((u1)code_blob_type); 1257 event.set_startAddress((u8)heap->low_boundary()); 1258 event.set_commitedTopAddress((u8)heap->high()); 1259 event.set_reservedTopAddress((u8)heap->high_boundary()); 1260 event.set_entryCount(heap->blob_count()); 1261 event.set_methodCount(heap->nmethod_count()); 1262 event.set_adaptorCount(heap->adapter_count()); 1263 event.set_unallocatedCapacity(heap->unallocated_capacity()/K); 1264 event.set_fullCount(heap->full_count()); 1265 event.commit(); 1266 } 1267 } 1268 1269 void CodeCache::print_memory_overhead() { 1270 size_t wasted_bytes = 0; 1271 FOR_ALL_HEAPS(heap) { 1272 CodeHeap* curr_heap = *heap; 1273 for (CodeBlob* cb = (CodeBlob*)curr_heap->first(); cb != NULL; cb = (CodeBlob*)curr_heap->next(cb)) { 1274 HeapBlock* heap_block = ((HeapBlock*)cb) - 1; 1275 wasted_bytes += heap_block->length() * CodeCacheSegmentSize - cb->size(); 1276 } 1277 } 1278 // Print bytes that are allocated in the freelist 1279 ttyLocker ttl; 1280 tty->print_cr("Number of elements in freelist: " SSIZE_FORMAT, freelists_length()); 1281 tty->print_cr("Allocated in freelist: " SSIZE_FORMAT "kB", bytes_allocated_in_freelists()/K); 1282 tty->print_cr("Unused bytes in CodeBlobs: " SSIZE_FORMAT "kB", (wasted_bytes/K)); 1283 tty->print_cr("Segment map size: " SSIZE_FORMAT "kB", allocated_segments()/K); // 1 byte per segment 1284 } 1285 1286 //------------------------------------------------------------------------------------------------ 1287 // Non-product version 1288 1289 #ifndef PRODUCT 1290 1291 void CodeCache::print_trace(const char* event, CodeBlob* cb, int size) { 1292 if (PrintCodeCache2) { // Need to add a new flag 1293 ResourceMark rm; 1294 if (size == 0) size = cb->size(); 1295 tty->print_cr("CodeCache %s: addr: " INTPTR_FORMAT ", size: 0x%x", event, p2i(cb), size); 1296 } 1297 } 1298 1299 void CodeCache::print_internals() { 1300 int nmethodCount = 0; 1301 int runtimeStubCount = 0; 1302 int adapterCount = 0; 1303 int deoptimizationStubCount = 0; 1304 int uncommonTrapStubCount = 0; 1305 int bufferBlobCount = 0; 1306 int total = 0; 1307 int nmethodAlive = 0; 1308 int nmethodNotEntrant = 0; 1309 int nmethodZombie = 0; 1310 int nmethodUnloaded = 0; 1311 int nmethodJava = 0; 1312 int nmethodNative = 0; 1313 int max_nm_size = 0; 1314 ResourceMark rm; 1315 1316 int i = 0; 1317 FOR_ALL_HEAPS(heap) { 1318 if ((_heaps->length() >= 1) && Verbose) { 1319 tty->print_cr("-- %s --", (*heap)->name()); 1320 } 1321 FOR_ALL_BLOBS(cb, *heap) { 1322 total++; 1323 if (cb->is_nmethod()) { 1324 nmethod* nm = (nmethod*)cb; 1325 1326 if (Verbose && nm->method() != NULL) { 1327 ResourceMark rm; 1328 char *method_name = nm->method()->name_and_sig_as_C_string(); 1329 tty->print("%s", method_name); 1330 if(nm->is_alive()) { tty->print_cr(" alive"); } 1331 if(nm->is_not_entrant()) { tty->print_cr(" not-entrant"); } 1332 if(nm->is_zombie()) { tty->print_cr(" zombie"); } 1333 } 1334 1335 nmethodCount++; 1336 1337 if(nm->is_alive()) { nmethodAlive++; } 1338 if(nm->is_not_entrant()) { nmethodNotEntrant++; } 1339 if(nm->is_zombie()) { nmethodZombie++; } 1340 if(nm->is_unloaded()) { nmethodUnloaded++; } 1341 if(nm->method() != NULL && nm->is_native_method()) { nmethodNative++; } 1342 1343 if(nm->method() != NULL && nm->is_java_method()) { 1344 nmethodJava++; 1345 max_nm_size = MAX2(max_nm_size, nm->size()); 1346 } 1347 } else if (cb->is_runtime_stub()) { 1348 runtimeStubCount++; 1349 } else if (cb->is_deoptimization_stub()) { 1350 deoptimizationStubCount++; 1351 } else if (cb->is_uncommon_trap_stub()) { 1352 uncommonTrapStubCount++; 1353 } else if (cb->is_adapter_blob()) { 1354 adapterCount++; 1355 } else if (cb->is_buffer_blob()) { 1356 bufferBlobCount++; 1357 } 1358 } 1359 } 1360 1361 int bucketSize = 512; 1362 int bucketLimit = max_nm_size / bucketSize + 1; 1363 int *buckets = NEW_C_HEAP_ARRAY(int, bucketLimit, mtCode); 1364 memset(buckets, 0, sizeof(int) * bucketLimit); 1365 1366 NMethodIterator iter; 1367 while(iter.next()) { 1368 nmethod* nm = iter.method(); 1369 if(nm->method() != NULL && nm->is_java_method()) { 1370 buckets[nm->size() / bucketSize]++; 1371 } 1372 } 1373 1374 tty->print_cr("Code Cache Entries (total of %d)",total); 1375 tty->print_cr("-------------------------------------------------"); 1376 tty->print_cr("nmethods: %d",nmethodCount); 1377 tty->print_cr("\talive: %d",nmethodAlive); 1378 tty->print_cr("\tnot_entrant: %d",nmethodNotEntrant); 1379 tty->print_cr("\tzombie: %d",nmethodZombie); 1380 tty->print_cr("\tunloaded: %d",nmethodUnloaded); 1381 tty->print_cr("\tjava: %d",nmethodJava); 1382 tty->print_cr("\tnative: %d",nmethodNative); 1383 tty->print_cr("runtime_stubs: %d",runtimeStubCount); 1384 tty->print_cr("adapters: %d",adapterCount); 1385 tty->print_cr("buffer blobs: %d",bufferBlobCount); 1386 tty->print_cr("deoptimization_stubs: %d",deoptimizationStubCount); 1387 tty->print_cr("uncommon_traps: %d",uncommonTrapStubCount); 1388 tty->print_cr("\nnmethod size distribution (non-zombie java)"); 1389 tty->print_cr("-------------------------------------------------"); 1390 1391 for(int i=0; i<bucketLimit; i++) { 1392 if(buckets[i] != 0) { 1393 tty->print("%d - %d bytes",i*bucketSize,(i+1)*bucketSize); 1394 tty->fill_to(40); 1395 tty->print_cr("%d",buckets[i]); 1396 } 1397 } 1398 1399 FREE_C_HEAP_ARRAY(int, buckets); 1400 print_memory_overhead(); 1401 } 1402 1403 #endif // !PRODUCT 1404 1405 void CodeCache::print() { 1406 print_summary(tty); 1407 1408 #ifndef PRODUCT 1409 if (!Verbose) return; 1410 1411 CodeBlob_sizes live; 1412 CodeBlob_sizes dead; 1413 1414 FOR_ALL_HEAPS(heap) { 1415 FOR_ALL_BLOBS(cb, *heap) { 1416 if (!cb->is_alive()) { 1417 dead.add(cb); 1418 } else { 1419 live.add(cb); 1420 } 1421 } 1422 } 1423 1424 tty->print_cr("CodeCache:"); 1425 tty->print_cr("nmethod dependency checking time %fs", dependentCheckTime.seconds()); 1426 1427 if (!live.is_empty()) { 1428 live.print("live"); 1429 } 1430 if (!dead.is_empty()) { 1431 dead.print("dead"); 1432 } 1433 1434 if (WizardMode) { 1435 // print the oop_map usage 1436 int code_size = 0; 1437 int number_of_blobs = 0; 1438 int number_of_oop_maps = 0; 1439 int map_size = 0; 1440 FOR_ALL_HEAPS(heap) { 1441 FOR_ALL_BLOBS(cb, *heap) { 1442 if (cb->is_alive()) { 1443 number_of_blobs++; 1444 code_size += cb->code_size(); 1445 ImmutableOopMapSet* set = cb->oop_maps(); 1446 if (set != NULL) { 1447 number_of_oop_maps += set->count(); 1448 map_size += set->nr_of_bytes(); 1449 } 1450 } 1451 } 1452 } 1453 tty->print_cr("OopMaps"); 1454 tty->print_cr(" #blobs = %d", number_of_blobs); 1455 tty->print_cr(" code size = %d", code_size); 1456 tty->print_cr(" #oop_maps = %d", number_of_oop_maps); 1457 tty->print_cr(" map size = %d", map_size); 1458 } 1459 1460 #endif // !PRODUCT 1461 } 1462 1463 void CodeCache::print_summary(outputStream* st, bool detailed) { 1464 FOR_ALL_HEAPS(heap_iterator) { 1465 CodeHeap* heap = (*heap_iterator); 1466 size_t total = (heap->high_boundary() - heap->low_boundary()); 1467 if (_heaps->length() >= 1) { 1468 st->print("%s:", heap->name()); 1469 } else { 1470 st->print("CodeCache:"); 1471 } 1472 st->print_cr(" size=" SIZE_FORMAT "Kb used=" SIZE_FORMAT 1473 "Kb max_used=" SIZE_FORMAT "Kb free=" SIZE_FORMAT "Kb", 1474 total/K, (total - heap->unallocated_capacity())/K, 1475 heap->max_allocated_capacity()/K, heap->unallocated_capacity()/K); 1476 1477 if (detailed) { 1478 st->print_cr(" bounds [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT "]", 1479 p2i(heap->low_boundary()), 1480 p2i(heap->high()), 1481 p2i(heap->high_boundary())); 1482 } 1483 } 1484 1485 if (detailed) { 1486 st->print_cr(" total_blobs=" UINT32_FORMAT " nmethods=" UINT32_FORMAT 1487 " adapters=" UINT32_FORMAT, 1488 blob_count(), nmethod_count(), adapter_count()); 1489 st->print_cr(" compilation: %s", CompileBroker::should_compile_new_jobs() ? 1490 "enabled" : Arguments::mode() == Arguments::_int ? 1491 "disabled (interpreter mode)" : 1492 "disabled (not enough contiguous free space left)"); 1493 } 1494 } 1495 1496 void CodeCache::print_codelist(outputStream* st) { 1497 assert_locked_or_safepoint(CodeCache_lock); 1498 1499 NMethodIterator iter; 1500 while(iter.next_alive()) { 1501 nmethod* nm = iter.method(); 1502 ResourceMark rm; 1503 char *method_name = nm->method()->name_and_sig_as_C_string(); 1504 st->print_cr("%d %d %s [" INTPTR_FORMAT ", " INTPTR_FORMAT " - " INTPTR_FORMAT "]", 1505 nm->compile_id(), nm->comp_level(), method_name, (intptr_t)nm->header_begin(), 1506 (intptr_t)nm->code_begin(), (intptr_t)nm->code_end()); 1507 } 1508 } 1509 1510 void CodeCache::print_layout(outputStream* st) { 1511 assert_locked_or_safepoint(CodeCache_lock); 1512 ResourceMark rm; 1513 1514 print_summary(st, true); 1515 } 1516 1517 void CodeCache::log_state(outputStream* st) { 1518 st->print(" total_blobs='" UINT32_FORMAT "' nmethods='" UINT32_FORMAT "'" 1519 " adapters='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'", 1520 blob_count(), nmethod_count(), adapter_count(), 1521 unallocated_capacity()); 1522 }