1 /*
   2  * Copyright (c) 1997, 2025, 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 "code/codeBlob.hpp"
  26 #include "code/codeCache.hpp"
  27 #include "code/codeHeapState.hpp"
  28 #include "code/compiledIC.hpp"
  29 #include "code/dependencies.hpp"
  30 #include "code/dependencyContext.hpp"
  31 #include "code/nmethod.hpp"
  32 #include "code/pcDesc.hpp"
  33 #include "compiler/compilationPolicy.hpp"
  34 #include "compiler/compileBroker.hpp"
  35 #include "compiler/compilerDefinitions.inline.hpp"
  36 #include "compiler/oopMap.hpp"
  37 #include "gc/shared/barrierSetNMethod.hpp"
  38 #include "gc/shared/classUnloadingContext.hpp"
  39 #include "gc/shared/collectedHeap.hpp"
  40 #include "jfr/jfrEvents.hpp"
  41 #include "jvm_io.h"
  42 #include "logging/log.hpp"
  43 #include "logging/logStream.hpp"
  44 #include "memory/allocation.inline.hpp"
  45 #include "memory/iterator.hpp"
  46 #include "memory/memoryReserver.hpp"
  47 #include "memory/resourceArea.hpp"
  48 #include "memory/universe.hpp"
  49 #include "oops/method.inline.hpp"
  50 #include "oops/objArrayOop.hpp"
  51 #include "oops/oop.inline.hpp"
  52 #include "oops/verifyOopClosure.hpp"
  53 #include "runtime/arguments.hpp"
  54 #include "runtime/atomic.hpp"
  55 #include "runtime/deoptimization.hpp"
  56 #include "runtime/globals_extension.hpp"
  57 #include "runtime/handles.inline.hpp"
  58 #include "runtime/icache.hpp"
  59 #include "runtime/init.hpp"
  60 #include "runtime/java.hpp"
  61 #include "runtime/mutexLocker.hpp"
  62 #include "runtime/os.inline.hpp"
  63 #include "runtime/safepointVerifiers.hpp"
  64 #include "runtime/vmThread.hpp"
  65 #include "sanitizers/leak.hpp"
  66 #include "services/memoryService.hpp"
  67 #include "utilities/align.hpp"
  68 #include "utilities/vmError.hpp"
  69 #include "utilities/xmlstream.hpp"
  70 #ifdef COMPILER1
  71 #include "c1/c1_Compilation.hpp"
  72 #include "c1/c1_Compiler.hpp"
  73 #endif
  74 #ifdef COMPILER2
  75 #include "opto/c2compiler.hpp"
  76 #include "opto/compile.hpp"
  77 #include "opto/node.hpp"
  78 #endif
  79 
  80 // Helper class for printing in CodeCache
  81 class CodeBlob_sizes {
  82  private:
  83   int count;
  84   int total_size;
  85   int header_size;
  86   int code_size;
  87   int stub_size;
  88   int relocation_size;
  89   int scopes_oop_size;
  90   int scopes_metadata_size;
  91   int scopes_data_size;
  92   int scopes_pcs_size;
  93 
  94  public:
  95   CodeBlob_sizes() {
  96     count            = 0;
  97     total_size       = 0;
  98     header_size      = 0;
  99     code_size        = 0;
 100     stub_size        = 0;
 101     relocation_size  = 0;
 102     scopes_oop_size  = 0;
 103     scopes_metadata_size  = 0;
 104     scopes_data_size = 0;
 105     scopes_pcs_size  = 0;
 106   }
 107 
 108   int total() const                              { return total_size; }
 109   bool is_empty() const                          { return count == 0; }
 110 
 111   void print(const char* title) const {
 112     if (is_empty()) {
 113       tty->print_cr(" #%d %s = %dK",
 114                     count,
 115                     title,
 116                     total()                 / (int)K);
 117     } else {
 118       tty->print_cr(" #%d %s = %dK (hdr %dK %d%%, loc %dK %d%%, code %dK %d%%, stub %dK %d%%, [oops %dK %d%%, metadata %dK %d%%, data %dK %d%%, pcs %dK %d%%])",
 119                     count,
 120                     title,
 121                     total()                 / (int)K,
 122                     header_size             / (int)K,
 123                     header_size             * 100 / total_size,
 124                     relocation_size         / (int)K,
 125                     relocation_size         * 100 / total_size,
 126                     code_size               / (int)K,
 127                     code_size               * 100 / total_size,
 128                     stub_size               / (int)K,
 129                     stub_size               * 100 / total_size,
 130                     scopes_oop_size         / (int)K,
 131                     scopes_oop_size         * 100 / total_size,
 132                     scopes_metadata_size    / (int)K,
 133                     scopes_metadata_size    * 100 / total_size,
 134                     scopes_data_size        / (int)K,
 135                     scopes_data_size        * 100 / total_size,
 136                     scopes_pcs_size         / (int)K,
 137                     scopes_pcs_size         * 100 / total_size);
 138     }
 139   }
 140 
 141   void add(CodeBlob* cb) {
 142     count++;
 143     total_size       += cb->size();
 144     header_size      += cb->header_size();
 145     relocation_size  += cb->relocation_size();
 146     if (cb->is_nmethod()) {
 147       nmethod* nm = cb->as_nmethod_or_null();
 148       code_size        += nm->insts_size();
 149       stub_size        += nm->stub_size();
 150 
 151       scopes_oop_size  += nm->oops_size();
 152       scopes_metadata_size  += nm->metadata_size();
 153       scopes_data_size += nm->scopes_data_size();
 154       scopes_pcs_size  += nm->scopes_pcs_size();
 155     } else {
 156       code_size        += cb->code_size();
 157     }
 158   }
 159 };
 160 
 161 // Iterate over all CodeHeaps
 162 #define FOR_ALL_HEAPS(heap) for (GrowableArrayIterator<CodeHeap*> heap = _heaps->begin(); heap != _heaps->end(); ++heap)
 163 #define FOR_ALL_ALLOCABLE_HEAPS(heap) for (GrowableArrayIterator<CodeHeap*> heap = _allocable_heaps->begin(); heap != _allocable_heaps->end(); ++heap)
 164 
 165 // Iterate over all CodeBlobs (cb) on the given CodeHeap
 166 #define FOR_ALL_BLOBS(cb, heap) for (CodeBlob* cb = first_blob(heap); cb != nullptr; cb = next_blob(heap, cb))
 167 
 168 address CodeCache::_low_bound = nullptr;
 169 address CodeCache::_high_bound = nullptr;
 170 volatile int CodeCache::_number_of_nmethods_with_dependencies = 0;
 171 ExceptionCache* volatile CodeCache::_exception_cache_purge_list = nullptr;
 172 
 173 // Initialize arrays of CodeHeap subsets
 174 GrowableArray<CodeHeap*>* CodeCache::_heaps = new(mtCode) GrowableArray<CodeHeap*> (static_cast<int>(CodeBlobType::All), mtCode);
 175 GrowableArray<CodeHeap*>* CodeCache::_nmethod_heaps = new(mtCode) GrowableArray<CodeHeap*> (static_cast<int>(CodeBlobType::All), mtCode);
 176 GrowableArray<CodeHeap*>* CodeCache::_allocable_heaps = new(mtCode) GrowableArray<CodeHeap*> (static_cast<int>(CodeBlobType::All), mtCode);
 177 
 178 static void check_min_size(const char* codeheap, size_t size, size_t required_size) {
 179   if (size < required_size) {
 180     log_debug(codecache)("Code heap (%s) size %zuK below required minimal size %zuK",
 181                          codeheap, size/K, required_size/K);
 182     err_msg title("Not enough space in %s to run VM", codeheap);
 183     err_msg message("%zuK < %zuK", size/K, required_size/K);
 184     vm_exit_during_initialization(title, message);
 185   }
 186 }
 187 
 188 struct CodeHeapInfo {
 189   size_t size;
 190   bool set;
 191   bool enabled;
 192 };
 193 
 194 static void set_size_of_unset_code_heap(CodeHeapInfo* heap, size_t available_size, size_t used_size, size_t min_size) {
 195   assert(!heap->set, "sanity");
 196   heap->size = (available_size > (used_size + min_size)) ? (available_size - used_size) : min_size;
 197 }
 198 
 199 void CodeCache::initialize_heaps() {
 200 
 201   CodeHeapInfo non_nmethod = {NonNMethodCodeHeapSize, FLAG_IS_CMDLINE(NonNMethodCodeHeapSize), true};
 202   CodeHeapInfo profiled = {ProfiledCodeHeapSize, FLAG_IS_CMDLINE(ProfiledCodeHeapSize), true};
 203   CodeHeapInfo non_profiled = {NonProfiledCodeHeapSize, FLAG_IS_CMDLINE(NonProfiledCodeHeapSize), true};
 204 
 205   const bool cache_size_set   = FLAG_IS_CMDLINE(ReservedCodeCacheSize);
 206   const size_t ps             = page_size(false, 8);
 207   const size_t min_size       = MAX2(os::vm_allocation_granularity(), ps);
 208   const size_t min_cache_size = CodeCacheMinimumUseSpace DEBUG_ONLY(* 3); // Make sure we have enough space for VM internal code
 209   size_t cache_size           = align_up(ReservedCodeCacheSize, min_size);
 210 
 211   // Prerequisites
 212   if (!heap_available(CodeBlobType::MethodProfiled)) {
 213     // For compatibility reasons, disabled tiered compilation overrides
 214     // segment size even if it is set explicitly.
 215     non_profiled.size += profiled.size;
 216     // Profiled code heap is not available, forcibly set size to 0
 217     profiled.size = 0;
 218     profiled.set = true;
 219     profiled.enabled = false;
 220   }
 221 
 222   assert(heap_available(CodeBlobType::MethodNonProfiled), "MethodNonProfiled heap is always available for segmented code heap");
 223 
 224   size_t compiler_buffer_size = 0;
 225   COMPILER1_PRESENT(compiler_buffer_size += CompilationPolicy::c1_count() * Compiler::code_buffer_size());
 226   COMPILER2_PRESENT(compiler_buffer_size += CompilationPolicy::c2_count() * C2Compiler::initial_code_buffer_size());
 227 
 228   if (!non_nmethod.set) {
 229     non_nmethod.size += compiler_buffer_size;
 230     // Further down, just before FLAG_SET_ERGO(), all segment sizes are
 231     // aligned down to the next lower multiple of min_size. For large page
 232     // sizes, this may result in (non_nmethod.size == 0) which is not acceptable.
 233     // Therefore, force non_nmethod.size to at least min_size.
 234     non_nmethod.size = MAX2(non_nmethod.size, min_size);
 235   }
 236 
 237   if (!profiled.set && !non_profiled.set) {
 238     non_profiled.size = profiled.size = (cache_size > non_nmethod.size + 2 * min_size) ?
 239                                         (cache_size - non_nmethod.size) / 2 : min_size;
 240   }
 241 
 242   if (profiled.set && !non_profiled.set) {
 243     set_size_of_unset_code_heap(&non_profiled, cache_size, non_nmethod.size + profiled.size, min_size);
 244   }
 245 
 246   if (!profiled.set && non_profiled.set) {
 247     set_size_of_unset_code_heap(&profiled, cache_size, non_nmethod.size + non_profiled.size, min_size);
 248   }
 249 
 250   // Compatibility.
 251   size_t non_nmethod_min_size = min_cache_size + compiler_buffer_size;
 252   if (!non_nmethod.set && profiled.set && non_profiled.set) {
 253     set_size_of_unset_code_heap(&non_nmethod, cache_size, profiled.size + non_profiled.size, non_nmethod_min_size);
 254   }
 255 
 256   size_t total = non_nmethod.size + profiled.size + non_profiled.size;
 257   if (total != cache_size && !cache_size_set) {
 258     log_info(codecache)("ReservedCodeCache size %zuK changed to total segments size NonNMethod "
 259                         "%zuK NonProfiled %zuK Profiled %zuK = %zuK",
 260                         cache_size/K, non_nmethod.size/K, non_profiled.size/K, profiled.size/K, total/K);
 261     // Adjust ReservedCodeCacheSize as necessary because it was not set explicitly
 262     cache_size = total;
 263   }
 264 
 265   log_debug(codecache)("Initializing code heaps ReservedCodeCache %zuK NonNMethod %zuK"
 266                        " NonProfiled %zuK Profiled %zuK",
 267                        cache_size/K, non_nmethod.size/K, non_profiled.size/K, profiled.size/K);
 268 
 269   // Validation
 270   // Check minimal required sizes
 271   check_min_size("non-nmethod code heap", non_nmethod.size, non_nmethod_min_size);
 272   if (profiled.enabled) {
 273     check_min_size("profiled code heap", profiled.size, min_size);
 274   }
 275   if (non_profiled.enabled) { // non_profiled.enabled is always ON for segmented code heap, leave it checked for clarity
 276     check_min_size("non-profiled code heap", non_profiled.size, min_size);
 277   }
 278   if (cache_size_set) {
 279     check_min_size("reserved code cache", cache_size, min_cache_size);
 280   }
 281 
 282   // ReservedCodeCacheSize was set explicitly, so report an error and abort if it doesn't match the segment sizes
 283   if (total != cache_size && cache_size_set) {
 284     err_msg message("NonNMethodCodeHeapSize (%zuK)", non_nmethod.size/K);
 285     if (profiled.enabled) {
 286       message.append(" + ProfiledCodeHeapSize (%zuK)", profiled.size/K);
 287     }
 288     if (non_profiled.enabled) {
 289       message.append(" + NonProfiledCodeHeapSize (%zuK)", non_profiled.size/K);
 290     }
 291     message.append(" = %zuK", total/K);
 292     message.append((total > cache_size) ? " is greater than " : " is less than ");
 293     message.append("ReservedCodeCacheSize (%zuK).", cache_size/K);
 294 
 295     vm_exit_during_initialization("Invalid code heap sizes", message);
 296   }
 297 
 298   // Compatibility. Print warning if using large pages but not able to use the size given
 299   if (UseLargePages) {
 300     const size_t lg_ps = page_size(false, 1);
 301     if (ps < lg_ps) {
 302       log_warning(codecache)("Code cache size too small for " PROPERFMT " pages. "
 303                              "Reverting to smaller page size (" PROPERFMT ").",
 304                              PROPERFMTARGS(lg_ps), PROPERFMTARGS(ps));
 305     }
 306   }
 307 
 308   // Note: if large page support is enabled, min_size is at least the large
 309   // page size. This ensures that the code cache is covered by large pages.
 310   non_profiled.size += non_nmethod.size & alignment_mask(min_size);
 311   non_profiled.size += profiled.size & alignment_mask(min_size);
 312   non_nmethod.size = align_down(non_nmethod.size, min_size);
 313   profiled.size = align_down(profiled.size, min_size);
 314   non_profiled.size = align_down(non_profiled.size, min_size);
 315 
 316   FLAG_SET_ERGO(NonNMethodCodeHeapSize, non_nmethod.size);
 317   FLAG_SET_ERGO(ProfiledCodeHeapSize, profiled.size);
 318   FLAG_SET_ERGO(NonProfiledCodeHeapSize, non_profiled.size);
 319   FLAG_SET_ERGO(ReservedCodeCacheSize, cache_size);
 320 
 321   ReservedSpace rs = reserve_heap_memory(cache_size, ps);
 322 
 323   // Register CodeHeaps with LSan as we sometimes embed pointers to malloc memory.
 324   LSAN_REGISTER_ROOT_REGION(rs.base(), rs.size());
 325 
 326   size_t offset = 0;
 327   if (profiled.enabled) {
 328     ReservedSpace profiled_space = rs.partition(offset, profiled.size);
 329     offset += profiled.size;
 330     // Tier 2 and tier 3 (profiled) methods
 331     add_heap(profiled_space, "CodeHeap 'profiled nmethods'", CodeBlobType::MethodProfiled);
 332   }
 333 
 334   ReservedSpace non_method_space = rs.partition(offset, non_nmethod.size);
 335   offset += non_nmethod.size;
 336   // Non-nmethods (stubs, adapters, ...)
 337   add_heap(non_method_space, "CodeHeap 'non-nmethods'", CodeBlobType::NonNMethod);
 338 
 339   if (non_profiled.enabled) {
 340     ReservedSpace non_profiled_space  = rs.partition(offset, non_profiled.size);
 341     // Tier 1 and tier 4 (non-profiled) methods and native methods
 342     add_heap(non_profiled_space, "CodeHeap 'non-profiled nmethods'", CodeBlobType::MethodNonProfiled);
 343   }
 344 }
 345 
 346 size_t CodeCache::page_size(bool aligned, size_t min_pages) {
 347   return aligned ? os::page_size_for_region_aligned(ReservedCodeCacheSize, min_pages) :
 348                    os::page_size_for_region_unaligned(ReservedCodeCacheSize, min_pages);
 349 }
 350 
 351 ReservedSpace CodeCache::reserve_heap_memory(size_t size, size_t rs_ps) {
 352   // Align and reserve space for code cache
 353   const size_t rs_align = MAX2(rs_ps, os::vm_allocation_granularity());
 354   const size_t rs_size = align_up(size, rs_align);
 355 
 356   ReservedSpace rs = CodeMemoryReserver::reserve(rs_size, rs_align, rs_ps);
 357   if (!rs.is_reserved()) {
 358     vm_exit_during_initialization(err_msg("Could not reserve enough space for code cache (%zuK)",
 359                                           rs_size/K));
 360   }
 361 
 362   // Initialize bounds
 363   _low_bound = (address)rs.base();
 364   _high_bound = _low_bound + rs.size();
 365   return rs;
 366 }
 367 
 368 // Heaps available for allocation
 369 bool CodeCache::heap_available(CodeBlobType code_blob_type) {
 370   if (!SegmentedCodeCache) {
 371     // No segmentation: use a single code heap
 372     return (code_blob_type == CodeBlobType::All);
 373   } else if (CompilerConfig::is_interpreter_only()) {
 374     // Interpreter only: we don't need any method code heaps
 375     return (code_blob_type == CodeBlobType::NonNMethod);
 376   } else if (CompilerConfig::is_c1_profiling()) {
 377     // Tiered compilation: use all code heaps
 378     return (code_blob_type < CodeBlobType::All);
 379   } else {
 380     // No TieredCompilation: we only need the non-nmethod and non-profiled code heap
 381     return (code_blob_type == CodeBlobType::NonNMethod) ||
 382            (code_blob_type == CodeBlobType::MethodNonProfiled);
 383   }
 384 }
 385 
 386 const char* CodeCache::get_code_heap_flag_name(CodeBlobType code_blob_type) {
 387   switch(code_blob_type) {
 388   case CodeBlobType::NonNMethod:
 389     return "NonNMethodCodeHeapSize";
 390     break;
 391   case CodeBlobType::MethodNonProfiled:
 392     return "NonProfiledCodeHeapSize";
 393     break;
 394   case CodeBlobType::MethodProfiled:
 395     return "ProfiledCodeHeapSize";
 396     break;
 397   default:
 398     ShouldNotReachHere();
 399     return nullptr;
 400   }
 401 }
 402 
 403 int CodeCache::code_heap_compare(CodeHeap* const &lhs, CodeHeap* const &rhs) {
 404   if (lhs->code_blob_type() == rhs->code_blob_type()) {
 405     return (lhs > rhs) ? 1 : ((lhs < rhs) ? -1 : 0);
 406   } else {
 407     return static_cast<int>(lhs->code_blob_type()) - static_cast<int>(rhs->code_blob_type());
 408   }
 409 }
 410 
 411 void CodeCache::add_heap(CodeHeap* heap) {
 412   assert(!Universe::is_fully_initialized(), "late heap addition?");
 413 
 414   _heaps->insert_sorted<code_heap_compare>(heap);
 415 
 416   CodeBlobType type = heap->code_blob_type();
 417   if (code_blob_type_accepts_nmethod(type)) {
 418     _nmethod_heaps->insert_sorted<code_heap_compare>(heap);
 419   }
 420   if (code_blob_type_accepts_allocable(type)) {
 421     _allocable_heaps->insert_sorted<code_heap_compare>(heap);
 422   }
 423 }
 424 
 425 void CodeCache::add_heap(ReservedSpace rs, const char* name, CodeBlobType code_blob_type) {
 426   // Check if heap is needed
 427   if (!heap_available(code_blob_type)) {
 428     return;
 429   }
 430 
 431   // Create CodeHeap
 432   CodeHeap* heap = new CodeHeap(name, code_blob_type);
 433   add_heap(heap);
 434 
 435   // Reserve Space
 436   size_t size_initial = MIN2((size_t)InitialCodeCacheSize, rs.size());
 437   size_initial = align_up(size_initial, rs.page_size());
 438   if (!heap->reserve(rs, size_initial, CodeCacheSegmentSize)) {
 439     vm_exit_during_initialization(err_msg("Could not reserve enough space in %s (%zuK)",
 440                                           heap->name(), size_initial/K));
 441   }
 442 
 443   // Register the CodeHeap
 444   MemoryService::add_code_heap_memory_pool(heap, name);
 445 }
 446 
 447 CodeHeap* CodeCache::get_code_heap_containing(void* start) {
 448   FOR_ALL_HEAPS(heap) {
 449     if ((*heap)->contains(start)) {
 450       return *heap;
 451     }
 452   }
 453   return nullptr;
 454 }
 455 
 456 CodeHeap* CodeCache::get_code_heap(const void* cb) {
 457   assert(cb != nullptr, "CodeBlob is null");
 458   FOR_ALL_HEAPS(heap) {
 459     if ((*heap)->contains(cb)) {
 460       return *heap;
 461     }
 462   }
 463   ShouldNotReachHere();
 464   return nullptr;
 465 }
 466 
 467 CodeHeap* CodeCache::get_code_heap(CodeBlobType code_blob_type) {
 468   FOR_ALL_HEAPS(heap) {
 469     if ((*heap)->accepts(code_blob_type)) {
 470       return *heap;
 471     }
 472   }
 473   return nullptr;
 474 }
 475 
 476 CodeBlob* CodeCache::first_blob(CodeHeap* heap) {
 477   assert_locked_or_safepoint(CodeCache_lock);
 478   assert(heap != nullptr, "heap is null");
 479   return (CodeBlob*)heap->first();
 480 }
 481 
 482 CodeBlob* CodeCache::first_blob(CodeBlobType code_blob_type) {
 483   if (heap_available(code_blob_type)) {
 484     return first_blob(get_code_heap(code_blob_type));
 485   } else {
 486     return nullptr;
 487   }
 488 }
 489 
 490 CodeBlob* CodeCache::next_blob(CodeHeap* heap, CodeBlob* cb) {
 491   assert_locked_or_safepoint(CodeCache_lock);
 492   assert(heap != nullptr, "heap is null");
 493   return (CodeBlob*)heap->next(cb);
 494 }
 495 
 496 /**
 497  * Do not seize the CodeCache lock here--if the caller has not
 498  * already done so, we are going to lose bigtime, since the code
 499  * cache will contain a garbage CodeBlob until the caller can
 500  * run the constructor for the CodeBlob subclass he is busy
 501  * instantiating.
 502  */
 503 CodeBlob* CodeCache::allocate(uint size, CodeBlobType code_blob_type, bool handle_alloc_failure, CodeBlobType orig_code_blob_type) {
 504   assert_locked_or_safepoint(CodeCache_lock);
 505   assert(size > 0, "Code cache allocation request must be > 0");
 506   if (size == 0) {
 507     return nullptr;
 508   }
 509   CodeBlob* cb = nullptr;
 510 
 511   // Get CodeHeap for the given CodeBlobType
 512   CodeHeap* heap = get_code_heap(code_blob_type);
 513   assert(heap != nullptr, "heap is null");
 514 
 515   while (true) {
 516     cb = (CodeBlob*)heap->allocate(size);
 517     if (cb != nullptr) break;
 518     if (!heap->expand_by(CodeCacheExpansionSize)) {
 519       // Save original type for error reporting
 520       if (orig_code_blob_type == CodeBlobType::All) {
 521         orig_code_blob_type = code_blob_type;
 522       }
 523       // Expansion failed
 524       if (SegmentedCodeCache) {
 525         // Fallback solution: Try to store code in another code heap.
 526         // NonNMethod -> MethodNonProfiled -> MethodProfiled (-> MethodNonProfiled)
 527         CodeBlobType type = code_blob_type;
 528         switch (type) {
 529         case CodeBlobType::NonNMethod:
 530           type = CodeBlobType::MethodNonProfiled;
 531           break;
 532         case CodeBlobType::MethodNonProfiled:
 533           type = CodeBlobType::MethodProfiled;
 534           break;
 535         case CodeBlobType::MethodProfiled:
 536           // Avoid loop if we already tried that code heap
 537           if (type == orig_code_blob_type) {
 538             type = CodeBlobType::MethodNonProfiled;
 539           }
 540           break;
 541         default:
 542           break;
 543         }
 544         if (type != code_blob_type && type != orig_code_blob_type && heap_available(type)) {
 545           if (PrintCodeCacheExtension) {
 546             tty->print_cr("Extension of %s failed. Trying to allocate in %s.",
 547                           heap->name(), get_code_heap(type)->name());
 548           }
 549           return allocate(size, type, handle_alloc_failure, orig_code_blob_type);
 550         }
 551       }
 552       if (handle_alloc_failure) {
 553         MutexUnlocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 554         CompileBroker::handle_full_code_cache(orig_code_blob_type);
 555       }
 556       return nullptr;
 557     } else {
 558       OrderAccess::release(); // ensure heap expansion is visible to an asynchronous observer (e.g. CodeHeapPool::get_memory_usage())
 559     }
 560     if (PrintCodeCacheExtension) {
 561       ResourceMark rm;
 562       if (_nmethod_heaps->length() >= 1) {
 563         tty->print("%s", heap->name());
 564       } else {
 565         tty->print("CodeCache");
 566       }
 567       tty->print_cr(" extended to [" INTPTR_FORMAT ", " INTPTR_FORMAT "] (%zd bytes)",
 568                     (intptr_t)heap->low_boundary(), (intptr_t)heap->high(),
 569                     (address)heap->high() - (address)heap->low_boundary());
 570     }
 571   }
 572   print_trace("allocation", cb, size);
 573   return cb;
 574 }
 575 
 576 void CodeCache::free(CodeBlob* cb) {
 577   assert_locked_or_safepoint(CodeCache_lock);
 578   CodeHeap* heap = get_code_heap(cb);
 579   print_trace("free", cb);
 580   if (cb->is_nmethod()) {
 581     heap->set_nmethod_count(heap->nmethod_count() - 1);
 582     if (((nmethod *)cb)->has_dependencies()) {
 583       Atomic::dec(&_number_of_nmethods_with_dependencies);
 584     }
 585   }
 586   if (cb->is_adapter_blob()) {
 587     heap->set_adapter_count(heap->adapter_count() - 1);
 588   }
 589 
 590   cb->~CodeBlob();
 591   // Get heap for given CodeBlob and deallocate
 592   heap->deallocate(cb);
 593 
 594   assert(heap->blob_count() >= 0, "sanity check");
 595 }
 596 
 597 void CodeCache::free_unused_tail(CodeBlob* cb, size_t used) {
 598   assert_locked_or_safepoint(CodeCache_lock);
 599   guarantee(cb->is_buffer_blob() && strncmp("Interpreter", cb->name(), 11) == 0, "Only possible for interpreter!");
 600   print_trace("free_unused_tail", cb);
 601 
 602   // We also have to account for the extra space (i.e. header) used by the CodeBlob
 603   // which provides the memory (see BufferBlob::create() in codeBlob.cpp).
 604   used += CodeBlob::align_code_offset(cb->header_size());
 605 
 606   // Get heap for given CodeBlob and deallocate its unused tail
 607   get_code_heap(cb)->deallocate_tail(cb, used);
 608   // Adjust the sizes of the CodeBlob
 609   cb->adjust_size(used);
 610 }
 611 
 612 void CodeCache::commit(CodeBlob* cb) {
 613   // this is called by nmethod::nmethod, which must already own CodeCache_lock
 614   assert_locked_or_safepoint(CodeCache_lock);
 615   CodeHeap* heap = get_code_heap(cb);
 616   if (cb->is_nmethod()) {
 617     heap->set_nmethod_count(heap->nmethod_count() + 1);
 618     if (((nmethod *)cb)->has_dependencies()) {
 619       Atomic::inc(&_number_of_nmethods_with_dependencies);
 620     }
 621   }
 622   if (cb->is_adapter_blob()) {
 623     heap->set_adapter_count(heap->adapter_count() + 1);
 624   }
 625 }
 626 
 627 bool CodeCache::contains(void *p) {
 628   // S390 uses contains() in current_frame(), which is used before
 629   // code cache initialization if NativeMemoryTracking=detail is set.
 630   S390_ONLY(if (_heaps == nullptr) return false;)
 631   // It should be ok to call contains without holding a lock.
 632   FOR_ALL_HEAPS(heap) {
 633     if ((*heap)->contains(p)) {
 634       return true;
 635     }
 636   }
 637   return false;
 638 }
 639 
 640 bool CodeCache::contains(nmethod *nm) {
 641   return contains((void *)nm);
 642 }
 643 
 644 // This method is safe to call without holding the CodeCache_lock. It only depends on the _segmap to contain
 645 // valid indices, which it will always do, as long as the CodeBlob is not in the process of being recycled.
 646 CodeBlob* CodeCache::find_blob(void* start) {
 647   // NMT can walk the stack before code cache is created
 648   if (_heaps != nullptr) {
 649     CodeHeap* heap = get_code_heap_containing(start);
 650     if (heap != nullptr) {
 651       return heap->find_blob(start);
 652     }
 653   }
 654   return nullptr;
 655 }
 656 
 657 nmethod* CodeCache::find_nmethod(void* start) {
 658   CodeBlob* cb = find_blob(start);
 659   assert(cb == nullptr || cb->is_nmethod(), "did not find an nmethod");
 660   return (nmethod*)cb;
 661 }
 662 
 663 void CodeCache::blobs_do(void f(CodeBlob* nm)) {
 664   assert_locked_or_safepoint(CodeCache_lock);
 665   FOR_ALL_HEAPS(heap) {
 666     FOR_ALL_BLOBS(cb, *heap) {
 667       f(cb);
 668     }
 669   }
 670 }
 671 
 672 void CodeCache::nmethods_do(void f(nmethod* nm)) {
 673   assert_locked_or_safepoint(CodeCache_lock);
 674   NMethodIterator iter(NMethodIterator::all);
 675   while(iter.next()) {
 676     f(iter.method());
 677   }
 678 }
 679 
 680 void CodeCache::nmethods_do(NMethodClosure* cl) {
 681   assert_locked_or_safepoint(CodeCache_lock);
 682   NMethodIterator iter(NMethodIterator::all);
 683   while(iter.next()) {
 684     cl->do_nmethod(iter.method());
 685   }
 686 }
 687 
 688 void CodeCache::metadata_do(MetadataClosure* f) {
 689   assert_locked_or_safepoint(CodeCache_lock);
 690   NMethodIterator iter(NMethodIterator::all);
 691   while(iter.next()) {
 692     iter.method()->metadata_do(f);
 693   }
 694 }
 695 
 696 // Calculate the number of GCs after which an nmethod is expected to have been
 697 // used in order to not be classed as cold.
 698 void CodeCache::update_cold_gc_count() {
 699   if (!MethodFlushing || !UseCodeCacheFlushing || NmethodSweepActivity == 0) {
 700     // No aging
 701     return;
 702   }
 703 
 704   size_t last_used = _last_unloading_used;
 705   double last_time = _last_unloading_time;
 706 
 707   double time = os::elapsedTime();
 708 
 709   size_t free = unallocated_capacity();
 710   size_t max = max_capacity();
 711   size_t used = max - free;
 712   double gc_interval = time - last_time;
 713 
 714   _unloading_threshold_gc_requested = false;
 715   _last_unloading_time = time;
 716   _last_unloading_used = used;
 717 
 718   if (last_time == 0.0) {
 719     // The first GC doesn't have enough information to make good
 720     // decisions, so just keep everything afloat
 721     log_info(codecache)("Unknown code cache pressure; don't age code");
 722     return;
 723   }
 724 
 725   if (gc_interval <= 0.0 || last_used >= used) {
 726     // Dodge corner cases where there is no pressure or negative pressure
 727     // on the code cache. Just don't unload when this happens.
 728     _cold_gc_count = INT_MAX;
 729     log_info(codecache)("No code cache pressure; don't age code");
 730     return;
 731   }
 732 
 733   double allocation_rate = (used - last_used) / gc_interval;
 734 
 735   _unloading_allocation_rates.add(allocation_rate);
 736   _unloading_gc_intervals.add(gc_interval);
 737 
 738   size_t aggressive_sweeping_free_threshold = StartAggressiveSweepingAt / 100.0 * max;
 739   if (free < aggressive_sweeping_free_threshold) {
 740     // We are already in the red zone; be very aggressive to avoid disaster
 741     // But not more aggressive than 2. This ensures that an nmethod must
 742     // have been unused at least between two GCs to be considered cold still.
 743     _cold_gc_count = 2;
 744     log_info(codecache)("Code cache critically low; use aggressive aging");
 745     return;
 746   }
 747 
 748   // The code cache has an expected time for cold nmethods to "time out"
 749   // when they have not been used. The time for nmethods to time out
 750   // depends on how long we expect we can keep allocating code until
 751   // aggressive sweeping starts, based on sampled allocation rates.
 752   double average_gc_interval = _unloading_gc_intervals.avg();
 753   double average_allocation_rate = _unloading_allocation_rates.avg();
 754   double time_to_aggressive = ((double)(free - aggressive_sweeping_free_threshold)) / average_allocation_rate;
 755   double cold_timeout = time_to_aggressive / NmethodSweepActivity;
 756 
 757   // Convert time to GC cycles, and crop at INT_MAX. The reason for
 758   // that is that the _cold_gc_count will be added to an epoch number
 759   // and that addition must not overflow, or we can crash the VM.
 760   // But not more aggressive than 2. This ensures that an nmethod must
 761   // have been unused at least between two GCs to be considered cold still.
 762   _cold_gc_count = MAX2(MIN2((uint64_t)(cold_timeout / average_gc_interval), (uint64_t)INT_MAX), (uint64_t)2);
 763 
 764   double used_ratio = double(used) / double(max);
 765   double last_used_ratio = double(last_used) / double(max);
 766   log_info(codecache)("Allocation rate: %.3f KB/s, time to aggressive unloading: %.3f s, cold timeout: %.3f s, cold gc count: " UINT64_FORMAT
 767                       ", used: %.3f MB (%.3f%%), last used: %.3f MB (%.3f%%), gc interval: %.3f s",
 768                       average_allocation_rate / K, time_to_aggressive, cold_timeout, _cold_gc_count,
 769                       double(used) / M, used_ratio * 100.0, double(last_used) / M, last_used_ratio * 100.0, average_gc_interval);
 770 
 771 }
 772 
 773 uint64_t CodeCache::cold_gc_count() {
 774   return _cold_gc_count;
 775 }
 776 
 777 void CodeCache::gc_on_allocation() {
 778   if (!is_init_completed()) {
 779     // Let's not heuristically trigger GCs before the JVM is ready for GCs, no matter what
 780     return;
 781   }
 782 
 783   size_t free = unallocated_capacity();
 784   size_t max = max_capacity();
 785   size_t used = max - free;
 786   double free_ratio = double(free) / double(max);
 787   if (free_ratio <= StartAggressiveSweepingAt / 100.0)  {
 788     // In case the GC is concurrent, we make sure only one thread requests the GC.
 789     if (Atomic::cmpxchg(&_unloading_threshold_gc_requested, false, true) == false) {
 790       log_info(codecache)("Triggering aggressive GC due to having only %.3f%% free memory", free_ratio * 100.0);
 791       Universe::heap()->collect(GCCause::_codecache_GC_aggressive);
 792     }
 793     return;
 794   }
 795 
 796   size_t last_used = _last_unloading_used;
 797   if (last_used >= used) {
 798     // No increase since last GC; no need to sweep yet
 799     return;
 800   }
 801   size_t allocated_since_last = used - last_used;
 802   double allocated_since_last_ratio = double(allocated_since_last) / double(max);
 803   double threshold = SweeperThreshold / 100.0;
 804   double used_ratio = double(used) / double(max);
 805   double last_used_ratio = double(last_used) / double(max);
 806   if (used_ratio > threshold) {
 807     // After threshold is reached, scale it by free_ratio so that more aggressive
 808     // GC is triggered as we approach code cache exhaustion
 809     threshold *= free_ratio;
 810   }
 811   // If code cache has been allocated without any GC at all, let's make sure
 812   // it is eventually invoked to avoid trouble.
 813   if (allocated_since_last_ratio > threshold) {
 814     // In case the GC is concurrent, we make sure only one thread requests the GC.
 815     if (Atomic::cmpxchg(&_unloading_threshold_gc_requested, false, true) == false) {
 816       log_info(codecache)("Triggering threshold (%.3f%%) GC due to allocating %.3f%% since last unloading (%.3f%% used -> %.3f%% used)",
 817                           threshold * 100.0, allocated_since_last_ratio * 100.0, last_used_ratio * 100.0, used_ratio * 100.0);
 818       Universe::heap()->collect(GCCause::_codecache_GC_threshold);
 819     }
 820   }
 821 }
 822 
 823 // We initialize the _gc_epoch to 2, because previous_completed_gc_marking_cycle
 824 // subtracts the value by 2, and the type is unsigned. We don't want underflow.
 825 //
 826 // Odd values mean that marking is in progress, and even values mean that no
 827 // marking is currently active.
 828 uint64_t CodeCache::_gc_epoch = 2;
 829 
 830 // How many GCs after an nmethod has not been used, do we consider it cold?
 831 uint64_t CodeCache::_cold_gc_count = INT_MAX;
 832 
 833 double CodeCache::_last_unloading_time = 0.0;
 834 size_t CodeCache::_last_unloading_used = 0;
 835 volatile bool CodeCache::_unloading_threshold_gc_requested = false;
 836 TruncatedSeq CodeCache::_unloading_gc_intervals(10 /* samples */);
 837 TruncatedSeq CodeCache::_unloading_allocation_rates(10 /* samples */);
 838 
 839 uint64_t CodeCache::gc_epoch() {
 840   return _gc_epoch;
 841 }
 842 
 843 bool CodeCache::is_gc_marking_cycle_active() {
 844   // Odd means that marking is active
 845   return (_gc_epoch % 2) == 1;
 846 }
 847 
 848 uint64_t CodeCache::previous_completed_gc_marking_cycle() {
 849   if (is_gc_marking_cycle_active()) {
 850     return _gc_epoch - 2;
 851   } else {
 852     return _gc_epoch - 1;
 853   }
 854 }
 855 
 856 void CodeCache::on_gc_marking_cycle_start() {
 857   assert(!is_gc_marking_cycle_active(), "Previous marking cycle never ended");
 858   ++_gc_epoch;
 859 }
 860 
 861 // Once started the code cache marking cycle must only be finished after marking of
 862 // the java heap is complete. Otherwise nmethods could appear to be not on stack even
 863 // if they have frames in continuation StackChunks that were not yet visited.
 864 void CodeCache::on_gc_marking_cycle_finish() {
 865   assert(is_gc_marking_cycle_active(), "Marking cycle started before last one finished");
 866   ++_gc_epoch;
 867   update_cold_gc_count();
 868 }
 869 
 870 void CodeCache::arm_all_nmethods() {
 871   BarrierSet::barrier_set()->barrier_set_nmethod()->arm_all_nmethods();
 872 }
 873 
 874 // Mark nmethods for unloading if they contain otherwise unreachable oops.
 875 void CodeCache::do_unloading(bool unloading_occurred) {
 876   assert_locked_or_safepoint(CodeCache_lock);
 877   NMethodIterator iter(NMethodIterator::all);
 878   while(iter.next()) {
 879     iter.method()->do_unloading(unloading_occurred);
 880   }
 881 }
 882 
 883 void CodeCache::verify_clean_inline_caches() {
 884 #ifdef ASSERT
 885   NMethodIterator iter(NMethodIterator::not_unloading);
 886   while(iter.next()) {
 887     nmethod* nm = iter.method();
 888     nm->verify_clean_inline_caches();
 889     nm->verify();
 890   }
 891 #endif
 892 }
 893 
 894 // Defer freeing of concurrently cleaned ExceptionCache entries until
 895 // after a global handshake operation.
 896 void CodeCache::release_exception_cache(ExceptionCache* entry) {
 897   if (SafepointSynchronize::is_at_safepoint()) {
 898     delete entry;
 899   } else {
 900     for (;;) {
 901       ExceptionCache* purge_list_head = Atomic::load(&_exception_cache_purge_list);
 902       entry->set_purge_list_next(purge_list_head);
 903       if (Atomic::cmpxchg(&_exception_cache_purge_list, purge_list_head, entry) == purge_list_head) {
 904         break;
 905       }
 906     }
 907   }
 908 }
 909 
 910 // Delete exception caches that have been concurrently unlinked,
 911 // followed by a global handshake operation.
 912 void CodeCache::purge_exception_caches() {
 913   ExceptionCache* curr = _exception_cache_purge_list;
 914   while (curr != nullptr) {
 915     ExceptionCache* next = curr->purge_list_next();
 916     delete curr;
 917     curr = next;
 918   }
 919   _exception_cache_purge_list = nullptr;
 920 }
 921 
 922 // Restart compiler if possible and required..
 923 void CodeCache::maybe_restart_compiler(size_t freed_memory) {
 924 
 925   // Try to start the compiler again if we freed any memory
 926   if (!CompileBroker::should_compile_new_jobs() && freed_memory != 0) {
 927     CompileBroker::set_should_compile_new_jobs(CompileBroker::run_compilation);
 928     log_info(codecache)("Restarting compiler");
 929     EventJITRestart event;
 930     event.set_freedMemory(freed_memory);
 931     event.set_codeCacheMaxCapacity(CodeCache::max_capacity());
 932     event.commit();
 933   }
 934 }
 935 
 936 uint8_t CodeCache::_unloading_cycle = 1;
 937 
 938 void CodeCache::increment_unloading_cycle() {
 939   // 2-bit value (see IsUnloadingState in nmethod.cpp for details)
 940   // 0 is reserved for new methods.
 941   _unloading_cycle = (_unloading_cycle + 1) % 4;
 942   if (_unloading_cycle == 0) {
 943     _unloading_cycle = 1;
 944   }
 945 }
 946 
 947 CodeCache::UnlinkingScope::UnlinkingScope(BoolObjectClosure* is_alive)
 948   : _is_unloading_behaviour(is_alive)
 949 {
 950   _saved_behaviour = IsUnloadingBehaviour::current();
 951   IsUnloadingBehaviour::set_current(&_is_unloading_behaviour);
 952   increment_unloading_cycle();
 953   DependencyContext::cleaning_start();
 954 }
 955 
 956 CodeCache::UnlinkingScope::~UnlinkingScope() {
 957   IsUnloadingBehaviour::set_current(_saved_behaviour);
 958   DependencyContext::cleaning_end();
 959 }
 960 
 961 void CodeCache::verify_oops() {
 962   MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 963   VerifyOopClosure voc;
 964   NMethodIterator iter(NMethodIterator::not_unloading);
 965   while(iter.next()) {
 966     nmethod* nm = iter.method();
 967     nm->oops_do(&voc);
 968     nm->verify_oop_relocations();
 969   }
 970 }
 971 
 972 int CodeCache::blob_count(CodeBlobType code_blob_type) {
 973   CodeHeap* heap = get_code_heap(code_blob_type);
 974   return (heap != nullptr) ? heap->blob_count() : 0;
 975 }
 976 
 977 int CodeCache::blob_count() {
 978   int count = 0;
 979   FOR_ALL_HEAPS(heap) {
 980     count += (*heap)->blob_count();
 981   }
 982   return count;
 983 }
 984 
 985 int CodeCache::nmethod_count(CodeBlobType code_blob_type) {
 986   CodeHeap* heap = get_code_heap(code_blob_type);
 987   return (heap != nullptr) ? heap->nmethod_count() : 0;
 988 }
 989 
 990 int CodeCache::nmethod_count() {
 991   int count = 0;
 992   for (CodeHeap* heap : *_nmethod_heaps) {
 993     count += heap->nmethod_count();
 994   }
 995   return count;
 996 }
 997 
 998 int CodeCache::adapter_count(CodeBlobType code_blob_type) {
 999   CodeHeap* heap = get_code_heap(code_blob_type);
1000   return (heap != nullptr) ? heap->adapter_count() : 0;
1001 }
1002 
1003 int CodeCache::adapter_count() {
1004   int count = 0;
1005   FOR_ALL_HEAPS(heap) {
1006     count += (*heap)->adapter_count();
1007   }
1008   return count;
1009 }
1010 
1011 address CodeCache::low_bound(CodeBlobType code_blob_type) {
1012   CodeHeap* heap = get_code_heap(code_blob_type);
1013   return (heap != nullptr) ? (address)heap->low_boundary() : nullptr;
1014 }
1015 
1016 address CodeCache::high_bound(CodeBlobType code_blob_type) {
1017   CodeHeap* heap = get_code_heap(code_blob_type);
1018   return (heap != nullptr) ? (address)heap->high_boundary() : nullptr;
1019 }
1020 
1021 size_t CodeCache::capacity() {
1022   size_t cap = 0;
1023   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1024     cap += (*heap)->capacity();
1025   }
1026   return cap;
1027 }
1028 
1029 size_t CodeCache::unallocated_capacity(CodeBlobType code_blob_type) {
1030   CodeHeap* heap = get_code_heap(code_blob_type);
1031   return (heap != nullptr) ? heap->unallocated_capacity() : 0;
1032 }
1033 
1034 size_t CodeCache::unallocated_capacity() {
1035   size_t unallocated_cap = 0;
1036   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1037     unallocated_cap += (*heap)->unallocated_capacity();
1038   }
1039   return unallocated_cap;
1040 }
1041 
1042 size_t CodeCache::max_capacity() {
1043   size_t max_cap = 0;
1044   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1045     max_cap += (*heap)->max_capacity();
1046   }
1047   return max_cap;
1048 }
1049 
1050 bool CodeCache::is_non_nmethod(address addr) {
1051   CodeHeap* blob = get_code_heap(CodeBlobType::NonNMethod);
1052   return blob->contains(addr);
1053 }
1054 
1055 size_t CodeCache::max_distance_to_non_nmethod() {
1056   if (!SegmentedCodeCache) {
1057     return ReservedCodeCacheSize;
1058   } else {
1059     CodeHeap* blob = get_code_heap(CodeBlobType::NonNMethod);
1060     // the max distance is minimized by placing the NonNMethod segment
1061     // in between MethodProfiled and MethodNonProfiled segments
1062     size_t dist1 = (size_t)blob->high() - (size_t)_low_bound;
1063     size_t dist2 = (size_t)_high_bound - (size_t)blob->low();
1064     return dist1 > dist2 ? dist1 : dist2;
1065   }
1066 }
1067 
1068 // Returns the reverse free ratio. E.g., if 25% (1/4) of the code cache
1069 // is free, reverse_free_ratio() returns 4.
1070 // Since code heap for each type of code blobs falls forward to the next
1071 // type of code heap, return the reverse free ratio for the entire
1072 // code cache.
1073 double CodeCache::reverse_free_ratio() {
1074   double unallocated = MAX2((double)unallocated_capacity(), 1.0); // Avoid division by 0;
1075   double max = (double)max_capacity();
1076   double result = max / unallocated;
1077   assert (max >= unallocated, "Must be");
1078   assert (result >= 1.0, "reverse_free_ratio must be at least 1. It is %f", result);
1079   return result;
1080 }
1081 
1082 size_t CodeCache::bytes_allocated_in_freelists() {
1083   size_t allocated_bytes = 0;
1084   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1085     allocated_bytes += (*heap)->allocated_in_freelist();
1086   }
1087   return allocated_bytes;
1088 }
1089 
1090 int CodeCache::allocated_segments() {
1091   int number_of_segments = 0;
1092   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1093     number_of_segments += (*heap)->allocated_segments();
1094   }
1095   return number_of_segments;
1096 }
1097 
1098 size_t CodeCache::freelists_length() {
1099   size_t length = 0;
1100   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1101     length += (*heap)->freelist_length();
1102   }
1103   return length;
1104 }
1105 
1106 void icache_init();
1107 
1108 void CodeCache::initialize() {
1109   assert(CodeCacheSegmentSize >= (uintx)CodeEntryAlignment, "CodeCacheSegmentSize must be large enough to align entry points");
1110 #ifdef COMPILER2
1111   assert(CodeCacheSegmentSize >= (uintx)OptoLoopAlignment,  "CodeCacheSegmentSize must be large enough to align inner loops");
1112 #endif
1113   assert(CodeCacheSegmentSize >= sizeof(jdouble),    "CodeCacheSegmentSize must be large enough to align constants");
1114   // This was originally just a check of the alignment, causing failure, instead, round
1115   // the code cache to the page size.  In particular, Solaris is moving to a larger
1116   // default page size.
1117   CodeCacheExpansionSize = align_up(CodeCacheExpansionSize, os::vm_page_size());
1118 
1119   if (SegmentedCodeCache) {
1120     // Use multiple code heaps
1121     initialize_heaps();
1122   } else {
1123     // Use a single code heap
1124     FLAG_SET_ERGO(NonNMethodCodeHeapSize, (uintx)os::vm_page_size());
1125     FLAG_SET_ERGO(ProfiledCodeHeapSize, 0);
1126     FLAG_SET_ERGO(NonProfiledCodeHeapSize, 0);
1127 
1128     // If InitialCodeCacheSize is equal to ReservedCodeCacheSize, then it's more likely
1129     // users want to use the largest available page.
1130     const size_t min_pages = (InitialCodeCacheSize == ReservedCodeCacheSize) ? 1 : 8;
1131     ReservedSpace rs = reserve_heap_memory(ReservedCodeCacheSize, page_size(false, min_pages));
1132     // Register CodeHeaps with LSan as we sometimes embed pointers to malloc memory.
1133     LSAN_REGISTER_ROOT_REGION(rs.base(), rs.size());
1134     add_heap(rs, "CodeCache", CodeBlobType::All);
1135   }
1136 
1137   // Initialize ICache flush mechanism
1138   // This service is needed for os::register_code_area
1139   icache_init();
1140 
1141   // Give OS a chance to register generated code area.
1142   // This is used on Windows 64 bit platforms to register
1143   // Structured Exception Handlers for our generated code.
1144   os::register_code_area((char*)low_bound(), (char*)high_bound());
1145 }
1146 
1147 void codeCache_init() {
1148   CodeCache::initialize();
1149 }
1150 
1151 //------------------------------------------------------------------------------------------------
1152 
1153 bool CodeCache::has_nmethods_with_dependencies() {
1154   return Atomic::load_acquire(&_number_of_nmethods_with_dependencies) != 0;
1155 }
1156 
1157 void CodeCache::clear_inline_caches() {
1158   assert_locked_or_safepoint(CodeCache_lock);
1159   NMethodIterator iter(NMethodIterator::not_unloading);
1160   while(iter.next()) {
1161     iter.method()->clear_inline_caches();
1162   }
1163 }
1164 
1165 // Only used by whitebox API
1166 void CodeCache::cleanup_inline_caches_whitebox() {
1167   assert_locked_or_safepoint(CodeCache_lock);
1168   NMethodIterator iter(NMethodIterator::not_unloading);
1169   while(iter.next()) {
1170     iter.method()->cleanup_inline_caches_whitebox();
1171   }
1172 }
1173 
1174 // Keeps track of time spent for checking dependencies
1175 NOT_PRODUCT(static elapsedTimer dependentCheckTime;)
1176 
1177 #ifndef PRODUCT
1178 // Check if any of live methods dependencies have been invalidated.
1179 // (this is expensive!)
1180 static void check_live_nmethods_dependencies(DepChange& changes) {
1181   // Checked dependencies are allocated into this ResourceMark
1182   ResourceMark rm;
1183 
1184   // Turn off dependency tracing while actually testing dependencies.
1185   FlagSetting fs(Dependencies::_verify_in_progress, true);
1186 
1187   typedef ResourceHashtable<DependencySignature, int, 11027,
1188                             AnyObj::RESOURCE_AREA, mtInternal,
1189                             &DependencySignature::hash,
1190                             &DependencySignature::equals> DepTable;
1191 
1192   DepTable* table = new DepTable();
1193 
1194   // Iterate over live nmethods and check dependencies of all nmethods that are not
1195   // marked for deoptimization. A particular dependency is only checked once.
1196   NMethodIterator iter(NMethodIterator::not_unloading);
1197   while(iter.next()) {
1198     nmethod* nm = iter.method();
1199     // Only notify for live nmethods
1200     if (!nm->is_marked_for_deoptimization()) {
1201       for (Dependencies::DepStream deps(nm); deps.next(); ) {
1202         // Construct abstraction of a dependency.
1203         DependencySignature* current_sig = new DependencySignature(deps);
1204 
1205         // Determine if dependency is already checked. table->put(...) returns
1206         // 'true' if the dependency is added (i.e., was not in the hashtable).
1207         if (table->put(*current_sig, 1)) {
1208           if (deps.check_dependency() != nullptr) {
1209             // Dependency checking failed. Print out information about the failed
1210             // dependency and finally fail with an assert. We can fail here, since
1211             // dependency checking is never done in a product build.
1212             tty->print_cr("Failed dependency:");
1213             changes.print();
1214             nm->print();
1215             nm->print_dependencies_on(tty);
1216             assert(false, "Should have been marked for deoptimization");
1217           }
1218         }
1219       }
1220     }
1221   }
1222 }
1223 #endif
1224 
1225 void CodeCache::mark_for_deoptimization(DeoptimizationScope* deopt_scope, KlassDepChange& changes) {
1226   MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1227 
1228   // search the hierarchy looking for nmethods which are affected by the loading of this class
1229 
1230   // then search the interfaces this class implements looking for nmethods
1231   // which might be dependent of the fact that an interface only had one
1232   // implementor.
1233   // nmethod::check_all_dependencies works only correctly, if no safepoint
1234   // can happen
1235   NoSafepointVerifier nsv;
1236   for (DepChange::ContextStream str(changes, nsv); str.next(); ) {
1237     InstanceKlass* d = str.klass();
1238     d->mark_dependent_nmethods(deopt_scope, changes);
1239   }
1240 
1241 #ifndef PRODUCT
1242   if (VerifyDependencies) {
1243     // Object pointers are used as unique identifiers for dependency arguments. This
1244     // is only possible if no safepoint, i.e., GC occurs during the verification code.
1245     dependentCheckTime.start();
1246     check_live_nmethods_dependencies(changes);
1247     dependentCheckTime.stop();
1248   }
1249 #endif
1250 }
1251 
1252 #if INCLUDE_JVMTI
1253 // RedefineClasses support for saving nmethods that are dependent on "old" methods.
1254 // We don't really expect this table to grow very large.  If it does, it can become a hashtable.
1255 static GrowableArray<nmethod*>* old_nmethod_table = nullptr;
1256 
1257 static void add_to_old_table(nmethod* c) {
1258   if (old_nmethod_table == nullptr) {
1259     old_nmethod_table = new (mtCode) GrowableArray<nmethod*>(100, mtCode);
1260   }
1261   old_nmethod_table->push(c);
1262 }
1263 
1264 static void reset_old_method_table() {
1265   if (old_nmethod_table != nullptr) {
1266     delete old_nmethod_table;
1267     old_nmethod_table = nullptr;
1268   }
1269 }
1270 
1271 // Remove this method when flushed.
1272 void CodeCache::unregister_old_nmethod(nmethod* c) {
1273   assert_lock_strong(CodeCache_lock);
1274   if (old_nmethod_table != nullptr) {
1275     int index = old_nmethod_table->find(c);
1276     if (index != -1) {
1277       old_nmethod_table->delete_at(index);
1278     }
1279   }
1280 }
1281 
1282 void CodeCache::old_nmethods_do(MetadataClosure* f) {
1283   // Walk old method table and mark those on stack.
1284   int length = 0;
1285   if (old_nmethod_table != nullptr) {
1286     length = old_nmethod_table->length();
1287     for (int i = 0; i < length; i++) {
1288       // Walk all methods saved on the last pass.  Concurrent class unloading may
1289       // also be looking at this method's metadata, so don't delete it yet if
1290       // it is marked as unloaded.
1291       old_nmethod_table->at(i)->metadata_do(f);
1292     }
1293   }
1294   log_debug(redefine, class, nmethod)("Walked %d nmethods for mark_on_stack", length);
1295 }
1296 
1297 // Walk compiled methods and mark dependent methods for deoptimization.
1298 void CodeCache::mark_dependents_for_evol_deoptimization(DeoptimizationScope* deopt_scope) {
1299   assert(SafepointSynchronize::is_at_safepoint(), "Can only do this at a safepoint!");
1300   // Each redefinition creates a new set of nmethods that have references to "old" Methods
1301   // So delete old method table and create a new one.
1302   reset_old_method_table();
1303 
1304   NMethodIterator iter(NMethodIterator::all);
1305   while(iter.next()) {
1306     nmethod* nm = iter.method();
1307     // Walk all alive nmethods to check for old Methods.
1308     // This includes methods whose inline caches point to old methods, so
1309     // inline cache clearing is unnecessary.
1310     if (nm->has_evol_metadata()) {
1311       deopt_scope->mark(nm);
1312       add_to_old_table(nm);
1313     }
1314   }
1315 }
1316 
1317 void CodeCache::mark_all_nmethods_for_evol_deoptimization(DeoptimizationScope* deopt_scope) {
1318   assert(SafepointSynchronize::is_at_safepoint(), "Can only do this at a safepoint!");
1319   NMethodIterator iter(NMethodIterator::all);
1320   while(iter.next()) {
1321     nmethod* nm = iter.method();
1322     if (!nm->method()->is_method_handle_intrinsic()) {
1323       if (nm->can_be_deoptimized()) {
1324         deopt_scope->mark(nm);
1325       }
1326       if (nm->has_evol_metadata()) {
1327         add_to_old_table(nm);
1328       }
1329     }
1330   }
1331 }
1332 
1333 #endif // INCLUDE_JVMTI
1334 
1335 // Mark methods for deopt (if safe or possible).
1336 void CodeCache::mark_all_nmethods_for_deoptimization(DeoptimizationScope* deopt_scope) {
1337   MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1338   NMethodIterator iter(NMethodIterator::not_unloading);
1339   while(iter.next()) {
1340     nmethod* nm = iter.method();
1341     if (!nm->is_native_method()) {
1342       deopt_scope->mark(nm);
1343     }
1344   }
1345 }
1346 
1347 void CodeCache::mark_for_deoptimization(DeoptimizationScope* deopt_scope, Method* dependee) {
1348   MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1349 
1350   NMethodIterator iter(NMethodIterator::not_unloading);
1351   while(iter.next()) {
1352     nmethod* nm = iter.method();
1353     if (nm->is_dependent_on_method(dependee)) {
1354       deopt_scope->mark(nm);
1355     }
1356   }
1357 }
1358 
1359 void CodeCache::make_marked_nmethods_deoptimized() {
1360   RelaxedNMethodIterator iter(RelaxedNMethodIterator::not_unloading);
1361   while(iter.next()) {
1362     nmethod* nm = iter.method();
1363     if (nm->is_marked_for_deoptimization() && !nm->has_been_deoptimized() && nm->can_be_deoptimized()) {
1364       nm->make_not_entrant("marked for deoptimization");
1365       nm->make_deoptimized();
1366     }
1367   }
1368 }
1369 
1370 // Marks compiled methods dependent on dependee.
1371 void CodeCache::mark_dependents_on(DeoptimizationScope* deopt_scope, InstanceKlass* dependee) {
1372   assert_lock_strong(Compile_lock);
1373 
1374   if (!has_nmethods_with_dependencies()) {
1375     return;
1376   }
1377 
1378   if (dependee->is_linked()) {
1379     // Class initialization state change.
1380     KlassInitDepChange changes(dependee);
1381     mark_for_deoptimization(deopt_scope, changes);
1382   } else {
1383     // New class is loaded.
1384     NewKlassDepChange changes(dependee);
1385     mark_for_deoptimization(deopt_scope, changes);
1386   }
1387 }
1388 
1389 // Marks compiled methods dependent on dependee
1390 void CodeCache::mark_dependents_on_method_for_breakpoint(const methodHandle& m_h) {
1391   assert(SafepointSynchronize::is_at_safepoint(), "invariant");
1392 
1393   DeoptimizationScope deopt_scope;
1394   // Compute the dependent nmethods
1395   mark_for_deoptimization(&deopt_scope, m_h());
1396   deopt_scope.deoptimize_marked();
1397 }
1398 
1399 void CodeCache::verify() {
1400   assert_locked_or_safepoint(CodeCache_lock);
1401   FOR_ALL_HEAPS(heap) {
1402     (*heap)->verify();
1403     FOR_ALL_BLOBS(cb, *heap) {
1404       cb->verify();
1405     }
1406   }
1407 }
1408 
1409 // A CodeHeap is full. Print out warning and report event.
1410 PRAGMA_DIAG_PUSH
1411 PRAGMA_FORMAT_NONLITERAL_IGNORED
1412 void CodeCache::report_codemem_full(CodeBlobType code_blob_type, bool print) {
1413   // Get nmethod heap for the given CodeBlobType and build CodeCacheFull event
1414   CodeHeap* heap = get_code_heap(code_blob_type);
1415   assert(heap != nullptr, "heap is null");
1416 
1417   int full_count = heap->report_full();
1418 
1419   if ((full_count == 1) || print) {
1420     // Not yet reported for this heap, report
1421     if (SegmentedCodeCache) {
1422       ResourceMark rm;
1423       stringStream msg1_stream, msg2_stream;
1424       msg1_stream.print("%s is full. Compiler has been disabled.",
1425                         get_code_heap_name(code_blob_type));
1426       msg2_stream.print("Try increasing the code heap size using -XX:%s=",
1427                  get_code_heap_flag_name(code_blob_type));
1428       const char *msg1 = msg1_stream.as_string();
1429       const char *msg2 = msg2_stream.as_string();
1430 
1431       log_warning(codecache)("%s", msg1);
1432       log_warning(codecache)("%s", msg2);
1433       warning("%s", msg1);
1434       warning("%s", msg2);
1435     } else {
1436       const char *msg1 = "CodeCache is full. Compiler has been disabled.";
1437       const char *msg2 = "Try increasing the code cache size using -XX:ReservedCodeCacheSize=";
1438 
1439       log_warning(codecache)("%s", msg1);
1440       log_warning(codecache)("%s", msg2);
1441       warning("%s", msg1);
1442       warning("%s", msg2);
1443     }
1444     stringStream s;
1445     // Dump code cache into a buffer before locking the tty.
1446     {
1447       MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1448       print_summary(&s);
1449     }
1450     {
1451       ttyLocker ttyl;
1452       tty->print("%s", s.freeze());
1453     }
1454 
1455     if (full_count == 1) {
1456       if (PrintCodeHeapAnalytics) {
1457         CompileBroker::print_heapinfo(tty, "all", 4096); // details, may be a lot!
1458       }
1459     }
1460   }
1461 
1462   EventCodeCacheFull event;
1463   if (event.should_commit()) {
1464     event.set_codeBlobType((u1)code_blob_type);
1465     event.set_startAddress((u8)heap->low_boundary());
1466     event.set_commitedTopAddress((u8)heap->high());
1467     event.set_reservedTopAddress((u8)heap->high_boundary());
1468     event.set_entryCount(heap->blob_count());
1469     event.set_methodCount(heap->nmethod_count());
1470     event.set_adaptorCount(heap->adapter_count());
1471     event.set_unallocatedCapacity(heap->unallocated_capacity());
1472     event.set_fullCount(heap->full_count());
1473     event.set_codeCacheMaxCapacity(CodeCache::max_capacity());
1474     event.commit();
1475   }
1476 }
1477 PRAGMA_DIAG_POP
1478 
1479 void CodeCache::print_memory_overhead() {
1480   size_t wasted_bytes = 0;
1481   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1482       CodeHeap* curr_heap = *heap;
1483       for (CodeBlob* cb = (CodeBlob*)curr_heap->first(); cb != nullptr; cb = (CodeBlob*)curr_heap->next(cb)) {
1484         HeapBlock* heap_block = ((HeapBlock*)cb) - 1;
1485         wasted_bytes += heap_block->length() * CodeCacheSegmentSize - cb->size();
1486       }
1487   }
1488   // Print bytes that are allocated in the freelist
1489   ttyLocker ttl;
1490   tty->print_cr("Number of elements in freelist: %zd",       freelists_length());
1491   tty->print_cr("Allocated in freelist:          %zdkB",  bytes_allocated_in_freelists()/K);
1492   tty->print_cr("Unused bytes in CodeBlobs:      %zdkB",  (wasted_bytes/K));
1493   tty->print_cr("Segment map size:               %zdkB",  allocated_segments()/K); // 1 byte per segment
1494 }
1495 
1496 //------------------------------------------------------------------------------------------------
1497 // Non-product version
1498 
1499 #ifndef PRODUCT
1500 
1501 void CodeCache::print_trace(const char* event, CodeBlob* cb, uint size) {
1502   if (PrintCodeCache2) {  // Need to add a new flag
1503     ResourceMark rm;
1504     if (size == 0) {
1505       int s = cb->size();
1506       assert(s >= 0, "CodeBlob size is negative: %d", s);
1507       size = (uint) s;
1508     }
1509     tty->print_cr("CodeCache %s:  addr: " INTPTR_FORMAT ", size: 0x%x", event, p2i(cb), size);
1510   }
1511 }
1512 
1513 void CodeCache::print_internals() {
1514   int nmethodCount = 0;
1515   int runtimeStubCount = 0;
1516   int upcallStubCount = 0;
1517   int adapterCount = 0;
1518   int mhAdapterCount = 0;
1519   int vtableBlobCount = 0;
1520   int deoptimizationStubCount = 0;
1521   int uncommonTrapStubCount = 0;
1522   int exceptionStubCount = 0;
1523   int safepointStubCount = 0;
1524   int bufferBlobCount = 0;
1525   int total = 0;
1526   int nmethodNotEntrant = 0;
1527   int nmethodJava = 0;
1528   int nmethodNative = 0;
1529   int max_nm_size = 0;
1530   ResourceMark rm;
1531 
1532   int i = 0;
1533   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1534     if ((_nmethod_heaps->length() >= 1) && Verbose) {
1535       tty->print_cr("-- %s --", (*heap)->name());
1536     }
1537     FOR_ALL_BLOBS(cb, *heap) {
1538       total++;
1539       if (cb->is_nmethod()) {
1540         nmethod* nm = (nmethod*)cb;
1541 
1542         if (Verbose && nm->method() != nullptr) {
1543           ResourceMark rm;
1544           char *method_name = nm->method()->name_and_sig_as_C_string();
1545           tty->print("%s", method_name);
1546           if(nm->is_not_entrant()) { tty->print_cr(" not-entrant"); }
1547         }
1548 
1549         nmethodCount++;
1550 
1551         if(nm->is_not_entrant()) { nmethodNotEntrant++; }
1552         if(nm->method() != nullptr && nm->is_native_method()) { nmethodNative++; }
1553 
1554         if(nm->method() != nullptr && nm->is_java_method()) {
1555           nmethodJava++;
1556           max_nm_size = MAX2(max_nm_size, nm->size());
1557         }
1558       } else if (cb->is_runtime_stub()) {
1559         runtimeStubCount++;
1560       } else if (cb->is_upcall_stub()) {
1561         upcallStubCount++;
1562       } else if (cb->is_deoptimization_stub()) {
1563         deoptimizationStubCount++;
1564       } else if (cb->is_uncommon_trap_stub()) {
1565         uncommonTrapStubCount++;
1566       } else if (cb->is_exception_stub()) {
1567         exceptionStubCount++;
1568       } else if (cb->is_safepoint_stub()) {
1569         safepointStubCount++;
1570       } else if (cb->is_adapter_blob()) {
1571         adapterCount++;
1572       } else if (cb->is_method_handles_adapter_blob()) {
1573         mhAdapterCount++;
1574       } else if (cb->is_vtable_blob()) {
1575         vtableBlobCount++;
1576       } else if (cb->is_buffer_blob()) {
1577         bufferBlobCount++;
1578       }
1579     }
1580   }
1581 
1582   int bucketSize = 512;
1583   int bucketLimit = max_nm_size / bucketSize + 1;
1584   int *buckets = NEW_C_HEAP_ARRAY(int, bucketLimit, mtCode);
1585   memset(buckets, 0, sizeof(int) * bucketLimit);
1586 
1587   NMethodIterator iter(NMethodIterator::all);
1588   while(iter.next()) {
1589     nmethod* nm = iter.method();
1590     if(nm->method() != nullptr && nm->is_java_method()) {
1591       buckets[nm->size() / bucketSize]++;
1592     }
1593   }
1594 
1595   tty->print_cr("Code Cache Entries (total of %d)",total);
1596   tty->print_cr("-------------------------------------------------");
1597   tty->print_cr("nmethods: %d",nmethodCount);
1598   tty->print_cr("\tnot_entrant: %d",nmethodNotEntrant);
1599   tty->print_cr("\tjava: %d",nmethodJava);
1600   tty->print_cr("\tnative: %d",nmethodNative);
1601   tty->print_cr("runtime_stubs: %d",runtimeStubCount);
1602   tty->print_cr("upcall_stubs: %d",upcallStubCount);
1603   tty->print_cr("adapters: %d",adapterCount);
1604   tty->print_cr("MH adapters: %d",mhAdapterCount);
1605   tty->print_cr("VTables: %d",vtableBlobCount);
1606   tty->print_cr("buffer blobs: %d",bufferBlobCount);
1607   tty->print_cr("deoptimization_stubs: %d",deoptimizationStubCount);
1608   tty->print_cr("uncommon_traps: %d",uncommonTrapStubCount);
1609   tty->print_cr("exception_stubs: %d",exceptionStubCount);
1610   tty->print_cr("safepoint_stubs: %d",safepointStubCount);
1611   tty->print_cr("\nnmethod size distribution");
1612   tty->print_cr("-------------------------------------------------");
1613 
1614   for(int i=0; i<bucketLimit; i++) {
1615     if(buckets[i] != 0) {
1616       tty->print("%d - %d bytes",i*bucketSize,(i+1)*bucketSize);
1617       tty->fill_to(40);
1618       tty->print_cr("%d",buckets[i]);
1619     }
1620   }
1621 
1622   FREE_C_HEAP_ARRAY(int, buckets);
1623   print_memory_overhead();
1624 }
1625 
1626 #endif // !PRODUCT
1627 
1628 void CodeCache::print() {
1629   print_summary(tty);
1630 
1631 #ifndef PRODUCT
1632   if (!Verbose) return;
1633 
1634   CodeBlob_sizes live[CompLevel_full_optimization + 1];
1635   CodeBlob_sizes runtimeStub;
1636   CodeBlob_sizes upcallStub;
1637   CodeBlob_sizes uncommonTrapStub;
1638   CodeBlob_sizes deoptimizationStub;
1639   CodeBlob_sizes exceptionStub;
1640   CodeBlob_sizes safepointStub;
1641   CodeBlob_sizes adapter;
1642   CodeBlob_sizes mhAdapter;
1643   CodeBlob_sizes vtableBlob;
1644   CodeBlob_sizes bufferBlob;
1645   CodeBlob_sizes other;
1646 
1647   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1648     FOR_ALL_BLOBS(cb, *heap) {
1649       if (cb->is_nmethod()) {
1650         const int level = cb->as_nmethod()->comp_level();
1651         assert(0 <= level && level <= CompLevel_full_optimization, "Invalid compilation level");
1652         live[level].add(cb);
1653       } else if (cb->is_runtime_stub()) {
1654         runtimeStub.add(cb);
1655       } else if (cb->is_upcall_stub()) {
1656         upcallStub.add(cb);
1657       } else if (cb->is_deoptimization_stub()) {
1658         deoptimizationStub.add(cb);
1659       } else if (cb->is_uncommon_trap_stub()) {
1660         uncommonTrapStub.add(cb);
1661       } else if (cb->is_exception_stub()) {
1662         exceptionStub.add(cb);
1663       } else if (cb->is_safepoint_stub()) {
1664         safepointStub.add(cb);
1665       } else if (cb->is_adapter_blob()) {
1666         adapter.add(cb);
1667       } else if (cb->is_method_handles_adapter_blob()) {
1668         mhAdapter.add(cb);
1669       } else if (cb->is_vtable_blob()) {
1670         vtableBlob.add(cb);
1671       } else if (cb->is_buffer_blob()) {
1672         bufferBlob.add(cb);
1673       } else {
1674         other.add(cb);
1675       }
1676     }
1677   }
1678 
1679   tty->print_cr("nmethod dependency checking time %fs", dependentCheckTime.seconds());
1680 
1681   tty->print_cr("nmethod blobs per compilation level:");
1682   for (int i = 0; i <= CompLevel_full_optimization; i++) {
1683     const char *level_name;
1684     switch (i) {
1685     case CompLevel_none:              level_name = "none";              break;
1686     case CompLevel_simple:            level_name = "simple";            break;
1687     case CompLevel_limited_profile:   level_name = "limited profile";   break;
1688     case CompLevel_full_profile:      level_name = "full profile";      break;
1689     case CompLevel_full_optimization: level_name = "full optimization"; break;
1690     default: assert(false, "invalid compilation level");
1691     }
1692     tty->print_cr("%s:", level_name);
1693     live[i].print("live");
1694   }
1695 
1696   struct {
1697     const char* name;
1698     const CodeBlob_sizes* sizes;
1699   } non_nmethod_blobs[] = {
1700     { "runtime",        &runtimeStub },
1701     { "upcall",         &upcallStub },
1702     { "uncommon trap",  &uncommonTrapStub },
1703     { "deoptimization", &deoptimizationStub },
1704     { "exception",      &exceptionStub },
1705     { "safepoint",      &safepointStub },
1706     { "adapter",        &adapter },
1707     { "mh_adapter",     &mhAdapter },
1708     { "vtable",         &vtableBlob },
1709     { "buffer blob",    &bufferBlob },
1710     { "other",          &other },
1711   };
1712   tty->print_cr("Non-nmethod blobs:");
1713   for (auto& blob: non_nmethod_blobs) {
1714     blob.sizes->print(blob.name);
1715   }
1716 
1717   if (WizardMode) {
1718      // print the oop_map usage
1719     int code_size = 0;
1720     int number_of_blobs = 0;
1721     int number_of_oop_maps = 0;
1722     int map_size = 0;
1723     FOR_ALL_ALLOCABLE_HEAPS(heap) {
1724       FOR_ALL_BLOBS(cb, *heap) {
1725         number_of_blobs++;
1726         code_size += cb->code_size();
1727         ImmutableOopMapSet* set = cb->oop_maps();
1728         if (set != nullptr) {
1729           number_of_oop_maps += set->count();
1730           map_size           += set->nr_of_bytes();
1731         }
1732       }
1733     }
1734     tty->print_cr("OopMaps");
1735     tty->print_cr("  #blobs    = %d", number_of_blobs);
1736     tty->print_cr("  code size = %d", code_size);
1737     tty->print_cr("  #oop_maps = %d", number_of_oop_maps);
1738     tty->print_cr("  map size  = %d", map_size);
1739   }
1740 
1741 #endif // !PRODUCT
1742 }
1743 
1744 void CodeCache::print_summary(outputStream* st, bool detailed) {
1745   int full_count = 0;
1746   julong total_used = 0;
1747   julong total_max_used = 0;
1748   julong total_free = 0;
1749   julong total_size = 0;
1750   FOR_ALL_HEAPS(heap_iterator) {
1751     CodeHeap* heap = (*heap_iterator);
1752     size_t total = (heap->high_boundary() - heap->low_boundary());
1753     if (_heaps->length() >= 1) {
1754       st->print("%s:", heap->name());
1755     } else {
1756       st->print("CodeCache:");
1757     }
1758     size_t size = total/K;
1759     size_t used = (total - heap->unallocated_capacity())/K;
1760     size_t max_used = heap->max_allocated_capacity()/K;
1761     size_t free = heap->unallocated_capacity()/K;
1762     total_size += size;
1763     total_used += used;
1764     total_max_used += max_used;
1765     total_free += free;
1766     st->print_cr(" size=%zuKb used=%zu"
1767                  "Kb max_used=%zuKb free=%zuKb",
1768                  size, used, max_used, free);
1769 
1770     if (detailed) {
1771       st->print_cr(" bounds [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT "]",
1772                    p2i(heap->low_boundary()),
1773                    p2i(heap->high()),
1774                    p2i(heap->high_boundary()));
1775 
1776       full_count += get_codemem_full_count(heap->code_blob_type());
1777     }
1778   }
1779 
1780   if (detailed) {
1781     if (SegmentedCodeCache) {
1782       st->print("CodeCache:");
1783       st->print_cr(" size=" JULONG_FORMAT "Kb, used=" JULONG_FORMAT
1784                    "Kb, max_used=" JULONG_FORMAT "Kb, free=" JULONG_FORMAT "Kb",
1785                    total_size, total_used, total_max_used, total_free);
1786     }
1787     st->print_cr(" total_blobs=" UINT32_FORMAT ", nmethods=" UINT32_FORMAT
1788                  ", adapters=" UINT32_FORMAT ", full_count=" UINT32_FORMAT,
1789                  blob_count(), nmethod_count(), adapter_count(), full_count);
1790     st->print_cr("Compilation: %s, stopped_count=%d, restarted_count=%d",
1791                  CompileBroker::should_compile_new_jobs() ?
1792                  "enabled" : Arguments::mode() == Arguments::_int ?
1793                  "disabled (interpreter mode)" :
1794                  "disabled (not enough contiguous free space left)",
1795                  CompileBroker::get_total_compiler_stopped_count(),
1796                  CompileBroker::get_total_compiler_restarted_count());
1797   }
1798 }
1799 
1800 void CodeCache::print_codelist(outputStream* st) {
1801   MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1802 
1803   NMethodIterator iter(NMethodIterator::not_unloading);
1804   while (iter.next()) {
1805     nmethod* nm = iter.method();
1806     ResourceMark rm;
1807     char* method_name = nm->method()->name_and_sig_as_C_string();
1808     const char* jvmci_name = nullptr;
1809 #if INCLUDE_JVMCI
1810     jvmci_name = nm->jvmci_name();
1811 #endif
1812     st->print_cr("%d %d %d %s%s%s [" INTPTR_FORMAT ", " INTPTR_FORMAT " - " INTPTR_FORMAT "]",
1813                  nm->compile_id(), nm->comp_level(), nm->get_state(),
1814                  method_name, jvmci_name ? " jvmci_name=" : "", jvmci_name ? jvmci_name : "",
1815                  (intptr_t)nm->header_begin(), (intptr_t)nm->code_begin(), (intptr_t)nm->code_end());
1816   }
1817 }
1818 
1819 void CodeCache::print_layout(outputStream* st) {
1820   MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1821   ResourceMark rm;
1822   print_summary(st, true);
1823 }
1824 
1825 void CodeCache::log_state(outputStream* st) {
1826   st->print(" total_blobs='" UINT32_FORMAT "' nmethods='" UINT32_FORMAT "'"
1827             " adapters='" UINT32_FORMAT "' free_code_cache='%zu'",
1828             blob_count(), nmethod_count(), adapter_count(),
1829             unallocated_capacity());
1830 }
1831 
1832 #ifdef LINUX
1833 void CodeCache::write_perf_map(const char* filename, outputStream* st) {
1834   MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1835   char fname[JVM_MAXPATHLEN];
1836   if (filename == nullptr) {
1837     // Invocation outside of jcmd requires pid substitution.
1838     if (!Arguments::copy_expand_pid(DEFAULT_PERFMAP_FILENAME,
1839                                     strlen(DEFAULT_PERFMAP_FILENAME),
1840                                     fname, JVM_MAXPATHLEN)) {
1841       st->print_cr("Warning: Not writing perf map as pid substitution failed.");
1842       return;
1843     }
1844     filename = fname;
1845   }
1846   fileStream fs(filename, "w");
1847   if (!fs.is_open()) {
1848     st->print_cr("Warning: Failed to create %s for perf map", filename);
1849     return;
1850   }
1851 
1852   AllCodeBlobsIterator iter(AllCodeBlobsIterator::not_unloading);
1853   while (iter.next()) {
1854     CodeBlob *cb = iter.method();
1855     ResourceMark rm;
1856     const char* method_name = nullptr;
1857     const char* jvmci_name = nullptr;
1858     if (cb->is_nmethod()) {
1859       nmethod* nm = cb->as_nmethod();
1860       method_name = nm->method()->external_name();
1861 #if INCLUDE_JVMCI
1862       jvmci_name = nm->jvmci_name();
1863 #endif
1864     } else {
1865       method_name = cb->name();
1866     }
1867     fs.print_cr(INTPTR_FORMAT " " INTPTR_FORMAT " %s%s%s",
1868                 (intptr_t)cb->code_begin(), (intptr_t)cb->code_size(),
1869                 method_name, jvmci_name ? " jvmci_name=" : "", jvmci_name ? jvmci_name : "");
1870   }
1871 }
1872 #endif // LINUX
1873 
1874 //---<  BEGIN  >--- CodeHeap State Analytics.
1875 
1876 void CodeCache::aggregate(outputStream *out, size_t granularity) {
1877   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1878     CodeHeapState::aggregate(out, (*heap), granularity);
1879   }
1880 }
1881 
1882 void CodeCache::discard(outputStream *out) {
1883   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1884     CodeHeapState::discard(out, (*heap));
1885   }
1886 }
1887 
1888 void CodeCache::print_usedSpace(outputStream *out) {
1889   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1890     CodeHeapState::print_usedSpace(out, (*heap));
1891   }
1892 }
1893 
1894 void CodeCache::print_freeSpace(outputStream *out) {
1895   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1896     CodeHeapState::print_freeSpace(out, (*heap));
1897   }
1898 }
1899 
1900 void CodeCache::print_count(outputStream *out) {
1901   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1902     CodeHeapState::print_count(out, (*heap));
1903   }
1904 }
1905 
1906 void CodeCache::print_space(outputStream *out) {
1907   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1908     CodeHeapState::print_space(out, (*heap));
1909   }
1910 }
1911 
1912 void CodeCache::print_age(outputStream *out) {
1913   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1914     CodeHeapState::print_age(out, (*heap));
1915   }
1916 }
1917 
1918 void CodeCache::print_names(outputStream *out) {
1919   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1920     CodeHeapState::print_names(out, (*heap));
1921   }
1922 }
1923 //---<  END  >--- CodeHeap State Analytics.