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