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) {
 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/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, 8), (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, size_t min_pages) {
 317   if (os::can_execute_large_page_memory()) {
 318     if (InitialCodeCacheSize < ReservedCodeCacheSize) {
 319       // Make sure that the page size allows for an incremental commit of the reserved space
 320       min_pages = MAX2(min_pages, (size_t)8);
 321     }
 322     return aligned ? os::page_size_for_region_aligned(ReservedCodeCacheSize, min_pages) :
 323                      os::page_size_for_region_unaligned(ReservedCodeCacheSize, min_pages);
 324   } else {
 325     return os::vm_page_size();
 326   }
 327 }
 328 
 329 ReservedCodeSpace CodeCache::reserve_heap_memory(size_t size) {
 330   // Align and reserve space for code cache
 331   const size_t rs_ps = page_size();
 332   const size_t rs_align = MAX2(rs_ps, (size_t) os::vm_allocation_granularity());
 333   const size_t rs_size = align_up(size, rs_align);
 334   ReservedCodeSpace rs(rs_size, rs_align, rs_ps > (size_t) os::vm_page_size());
 335   if (!rs.is_reserved()) {
 336     vm_exit_during_initialization(err_msg("Could not reserve enough space for code cache (" SIZE_FORMAT "K)",
 337                                           rs_size/K));
 338   }
 339 
 340   // Initialize bounds
 341   _low_bound = (address)rs.base();
 342   _high_bound = _low_bound + rs.size();
 343   return rs;
 344 }
 345 
 346 // Heaps available for allocation
 347 bool CodeCache::heap_available(int code_blob_type) {
 348   if (!SegmentedCodeCache) {
 349     // No segmentation: use a single code heap
 350     return (code_blob_type == CodeBlobType::All);
 351   } else if (Arguments::is_interpreter_only()) {
 352     // Interpreter only: we don't need any method code heaps
 353     return (code_blob_type == CodeBlobType::NonNMethod);
 354   } else if (TieredCompilation && (TieredStopAtLevel > CompLevel_simple)) {
 355     // Tiered compilation: use all code heaps
 356     return (code_blob_type < CodeBlobType::All);
 357   } else {
 358     // No TieredCompilation: we only need the non-nmethod and non-profiled code heap
 359     return (code_blob_type == CodeBlobType::NonNMethod) ||
 360            (code_blob_type == CodeBlobType::MethodNonProfiled);
 361   }
 362 }
 363 
 364 const char* CodeCache::get_code_heap_flag_name(int code_blob_type) {
 365   switch(code_blob_type) {
 366   case CodeBlobType::NonNMethod:
 367     return "NonNMethodCodeHeapSize";
 368     break;
 369   case CodeBlobType::MethodNonProfiled:
 370     return "NonProfiledCodeHeapSize";
 371     break;
 372   case CodeBlobType::MethodProfiled:
 373     return "ProfiledCodeHeapSize";
 374     break;
 375   }
 376   ShouldNotReachHere();
 377   return NULL;
 378 }
 379 
 380 int CodeCache::code_heap_compare(CodeHeap* const &lhs, CodeHeap* const &rhs) {
 381   if (lhs->code_blob_type() == rhs->code_blob_type()) {
 382     return (lhs > rhs) ? 1 : ((lhs < rhs) ? -1 : 0);
 383   } else {
 384     return lhs->code_blob_type() - rhs->code_blob_type();
 385   }
 386 }
 387 
 388 void CodeCache::add_heap(CodeHeap* heap) {
 389   assert(!Universe::is_fully_initialized(), "late heap addition?");
 390 
 391   _heaps->insert_sorted<code_heap_compare>(heap);
 392 
 393   int type = heap->code_blob_type();
 394   if (code_blob_type_accepts_compiled(type)) {
 395     _compiled_heaps->insert_sorted<code_heap_compare>(heap);
 396   }
 397   if (code_blob_type_accepts_nmethod(type)) {
 398     _nmethod_heaps->insert_sorted<code_heap_compare>(heap);
 399   }
 400   if (code_blob_type_accepts_allocable(type)) {
 401     _allocable_heaps->insert_sorted<code_heap_compare>(heap);
 402   }
 403 }
 404 
 405 void CodeCache::add_heap(ReservedSpace rs, const char* name, int code_blob_type) {
 406   // Check if heap is needed
 407   if (!heap_available(code_blob_type)) {
 408     return;
 409   }
 410 
 411   // Create CodeHeap
 412   CodeHeap* heap = new CodeHeap(name, code_blob_type);
 413   add_heap(heap);
 414 
 415   // Reserve Space
 416   size_t size_initial = MIN2((size_t)InitialCodeCacheSize, rs.size());
 417   size_initial = align_up(size_initial, os::vm_page_size());
 418   if (!heap->reserve(rs, size_initial, CodeCacheSegmentSize)) {
 419     vm_exit_during_initialization(err_msg("Could not reserve enough space in %s (" SIZE_FORMAT "K)",
 420                                           heap->name(), size_initial/K));
 421   }
 422 
 423   // Register the CodeHeap
 424   MemoryService::add_code_heap_memory_pool(heap, name);
 425 }
 426 
 427 CodeHeap* CodeCache::get_code_heap_containing(void* start) {
 428   FOR_ALL_HEAPS(heap) {
 429     if ((*heap)->contains(start)) {
 430       return *heap;
 431     }
 432   }
 433   return NULL;
 434 }
 435 
 436 CodeHeap* CodeCache::get_code_heap(const CodeBlob* cb) {
 437   assert(cb != NULL, "CodeBlob is null");
 438   FOR_ALL_HEAPS(heap) {
 439     if ((*heap)->contains_blob(cb)) {
 440       return *heap;
 441     }
 442   }
 443   ShouldNotReachHere();
 444   return NULL;
 445 }
 446 
 447 CodeHeap* CodeCache::get_code_heap(int code_blob_type) {
 448   FOR_ALL_HEAPS(heap) {
 449     if ((*heap)->accepts(code_blob_type)) {
 450       return *heap;
 451     }
 452   }
 453   return NULL;
 454 }
 455 
 456 CodeBlob* CodeCache::first_blob(CodeHeap* heap) {
 457   assert_locked_or_safepoint(CodeCache_lock);
 458   assert(heap != NULL, "heap is null");
 459   return (CodeBlob*)heap->first();
 460 }
 461 
 462 CodeBlob* CodeCache::first_blob(int code_blob_type) {
 463   if (heap_available(code_blob_type)) {
 464     return first_blob(get_code_heap(code_blob_type));
 465   } else {
 466     return NULL;
 467   }
 468 }
 469 
 470 CodeBlob* CodeCache::next_blob(CodeHeap* heap, CodeBlob* cb) {
 471   assert_locked_or_safepoint(CodeCache_lock);
 472   assert(heap != NULL, "heap is null");
 473   return (CodeBlob*)heap->next(cb);
 474 }
 475 
 476 /**
 477  * Do not seize the CodeCache lock here--if the caller has not
 478  * already done so, we are going to lose bigtime, since the code
 479  * cache will contain a garbage CodeBlob until the caller can
 480  * run the constructor for the CodeBlob subclass he is busy
 481  * instantiating.
 482  */
 483 CodeBlob* CodeCache::allocate(int size, int code_blob_type, int orig_code_blob_type) {
 484   // Possibly wakes up the sweeper thread.
 485   NMethodSweeper::notify(code_blob_type);
 486   assert_locked_or_safepoint(CodeCache_lock);
 487   assert(size > 0, "Code cache allocation request must be > 0 but is %d", size);
 488   if (size <= 0) {
 489     return NULL;
 490   }
 491   CodeBlob* cb = NULL;
 492 
 493   // Get CodeHeap for the given CodeBlobType
 494   CodeHeap* heap = get_code_heap(code_blob_type);
 495   assert(heap != NULL, "heap is null");
 496 
 497   while (true) {
 498     cb = (CodeBlob*)heap->allocate(size);
 499     if (cb != NULL) break;
 500     if (!heap->expand_by(CodeCacheExpansionSize)) {
 501       // Save original type for error reporting
 502       if (orig_code_blob_type == CodeBlobType::All) {
 503         orig_code_blob_type = code_blob_type;
 504       }
 505       // Expansion failed
 506       if (SegmentedCodeCache) {
 507         // Fallback solution: Try to store code in another code heap.
 508         // NonNMethod -> MethodNonProfiled -> MethodProfiled (-> MethodNonProfiled)
 509         // Note that in the sweeper, we check the reverse_free_ratio of the code heap
 510         // and force stack scanning if less than 10% of the code heap are free.
 511         int type = code_blob_type;
 512         switch (type) {
 513         case CodeBlobType::NonNMethod:
 514           type = CodeBlobType::MethodNonProfiled;
 515           break;
 516         case CodeBlobType::MethodNonProfiled:
 517           type = CodeBlobType::MethodProfiled;
 518           break;
 519         case CodeBlobType::MethodProfiled:
 520           // Avoid loop if we already tried that code heap
 521           if (type == orig_code_blob_type) {
 522             type = CodeBlobType::MethodNonProfiled;
 523           }
 524           break;
 525         }
 526         if (type != code_blob_type && type != orig_code_blob_type && heap_available(type)) {
 527           if (PrintCodeCacheExtension) {
 528             tty->print_cr("Extension of %s failed. Trying to allocate in %s.",
 529                           heap->name(), get_code_heap(type)->name());
 530           }
 531           return allocate(size, type, orig_code_blob_type);
 532         }
 533       }
 534       MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 535       CompileBroker::handle_full_code_cache(orig_code_blob_type);
 536       return NULL;
 537     }
 538     if (PrintCodeCacheExtension) {
 539       ResourceMark rm;
 540       if (_nmethod_heaps->length() >= 1) {
 541         tty->print("%s", heap->name());
 542       } else {
 543         tty->print("CodeCache");
 544       }
 545       tty->print_cr(" extended to [" INTPTR_FORMAT ", " INTPTR_FORMAT "] (" SSIZE_FORMAT " bytes)",
 546                     (intptr_t)heap->low_boundary(), (intptr_t)heap->high(),
 547                     (address)heap->high() - (address)heap->low_boundary());
 548     }
 549   }
 550   print_trace("allocation", cb, size);
 551   return cb;
 552 }
 553 
 554 void CodeCache::free(CodeBlob* cb) {
 555   assert_locked_or_safepoint(CodeCache_lock);
 556   CodeHeap* heap = get_code_heap(cb);
 557   print_trace("free", cb);
 558   if (cb->is_nmethod()) {
 559     heap->set_nmethod_count(heap->nmethod_count() - 1);
 560     if (((nmethod *)cb)->has_dependencies()) {
 561       _number_of_nmethods_with_dependencies--;
 562     }
 563   }
 564   if (cb->is_adapter_blob()) {
 565     heap->set_adapter_count(heap->adapter_count() - 1);
 566   }
 567 
 568   // Get heap for given CodeBlob and deallocate
 569   get_code_heap(cb)->deallocate(cb);
 570 
 571   assert(heap->blob_count() >= 0, "sanity check");
 572 }
 573 
 574 void CodeCache::free_unused_tail(CodeBlob* cb, size_t used) {
 575   assert_locked_or_safepoint(CodeCache_lock);
 576   guarantee(cb->is_buffer_blob() && strncmp("Interpreter", cb->name(), 11) == 0, "Only possible for interpreter!");
 577   print_trace("free_unused_tail", cb);
 578 
 579   // We also have to account for the extra space (i.e. header) used by the CodeBlob
 580   // which provides the memory (see BufferBlob::create() in codeBlob.cpp).
 581   used += CodeBlob::align_code_offset(cb->header_size());
 582 
 583   // Get heap for given CodeBlob and deallocate its unused tail
 584   get_code_heap(cb)->deallocate_tail(cb, used);
 585   // Adjust the sizes of the CodeBlob
 586   cb->adjust_size(used);
 587 }
 588 
 589 void CodeCache::commit(CodeBlob* cb) {
 590   // this is called by nmethod::nmethod, which must already own CodeCache_lock
 591   assert_locked_or_safepoint(CodeCache_lock);
 592   CodeHeap* heap = get_code_heap(cb);
 593   if (cb->is_nmethod()) {
 594     heap->set_nmethod_count(heap->nmethod_count() + 1);
 595     if (((nmethod *)cb)->has_dependencies()) {
 596       _number_of_nmethods_with_dependencies++;
 597     }
 598   }
 599   if (cb->is_adapter_blob()) {
 600     heap->set_adapter_count(heap->adapter_count() + 1);
 601   }
 602 
 603   // flush the hardware I-cache
 604   ICache::invalidate_range(cb->content_begin(), cb->content_size());
 605 }
 606 
 607 bool CodeCache::contains(void *p) {
 608   // S390 uses contains() in current_frame(), which is used before
 609   // code cache initialization if NativeMemoryTracking=detail is set.
 610   S390_ONLY(if (_heaps == NULL) return false;)
 611   // It should be ok to call contains without holding a lock.
 612   FOR_ALL_HEAPS(heap) {
 613     if ((*heap)->contains(p)) {
 614       return true;
 615     }
 616   }
 617   return false;
 618 }
 619 
 620 bool CodeCache::contains(nmethod *nm) {
 621   return contains((void *)nm);
 622 }
 623 
 624 // This method is safe to call without holding the CodeCache_lock, as long as a dead CodeBlob is not
 625 // looked up (i.e., one that has been marked for deletion). It only depends on the _segmap to contain
 626 // valid indices, which it will always do, as long as the CodeBlob is not in the process of being recycled.
 627 CodeBlob* CodeCache::find_blob(void* start) {
 628   CodeBlob* result = find_blob_unsafe(start);
 629   // We could potentially look up non_entrant methods
 630   guarantee(result == NULL || !result->is_zombie() || result->is_locked_by_vm() || VMError::is_error_reported(), "unsafe access to zombie method");
 631   return result;
 632 }
 633 
 634 // Lookup that does not fail if you lookup a zombie method (if you call this, be sure to know
 635 // what you are doing)
 636 CodeBlob* CodeCache::find_blob_unsafe(void* start) {
 637   // NMT can walk the stack before code cache is created
 638   if (_heaps != NULL) {
 639     CodeHeap* heap = get_code_heap_containing(start);
 640     if (heap != NULL) {
 641       return heap->find_blob_unsafe(start);
 642     }
 643   }
 644   return NULL;
 645 }
 646 
 647 nmethod* CodeCache::find_nmethod(void* start) {
 648   CodeBlob* cb = find_blob(start);
 649   assert(cb->is_nmethod(), "did not find an nmethod");
 650   return (nmethod*)cb;
 651 }
 652 
 653 void CodeCache::blobs_do(void f(CodeBlob* nm)) {
 654   assert_locked_or_safepoint(CodeCache_lock);
 655   FOR_ALL_HEAPS(heap) {
 656     FOR_ALL_BLOBS(cb, *heap) {
 657       f(cb);
 658     }
 659   }
 660 }
 661 
 662 void CodeCache::nmethods_do(void f(nmethod* nm)) {
 663   assert_locked_or_safepoint(CodeCache_lock);
 664   NMethodIterator iter;
 665   while(iter.next()) {
 666     f(iter.method());
 667   }
 668 }
 669 
 670 void CodeCache::metadata_do(void f(Metadata* m)) {
 671   assert_locked_or_safepoint(CodeCache_lock);
 672   NMethodIterator iter;
 673   while(iter.next_alive()) {
 674     iter.method()->metadata_do(f);
 675   }
 676   AOTLoader::metadata_do(f);
 677 }
 678 
 679 int CodeCache::alignment_unit() {
 680   return (int)_heaps->first()->alignment_unit();
 681 }
 682 
 683 int CodeCache::alignment_offset() {
 684   return (int)_heaps->first()->alignment_offset();
 685 }
 686 
 687 // Mark nmethods for unloading if they contain otherwise unreachable oops.
 688 void CodeCache::do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred) {
 689   assert_locked_or_safepoint(CodeCache_lock);
 690   CompiledMethodIterator iter;
 691   while(iter.next_alive()) {
 692     iter.method()->do_unloading(is_alive);
 693   }
 694 
 695   // Now that all the unloaded nmethods are known, cleanup caches
 696   // before CLDG is purged.
 697   // This is another code cache walk but it is moved from gc_epilogue.
 698   // G1 does a parallel walk of the nmethods so cleans them up
 699   // as it goes and doesn't call this.
 700   do_unloading_nmethod_caches(unloading_occurred);
 701 }
 702 
 703 void CodeCache::blobs_do(CodeBlobClosure* f) {
 704   assert_locked_or_safepoint(CodeCache_lock);
 705   FOR_ALL_ALLOCABLE_HEAPS(heap) {
 706     FOR_ALL_BLOBS(cb, *heap) {
 707       if (cb->is_alive()) {
 708         f->do_code_blob(cb);
 709 #ifdef ASSERT
 710         if (cb->is_nmethod()) {
 711           Universe::heap()->verify_nmethod((nmethod*)cb);
 712         }
 713 #endif //ASSERT
 714       }
 715     }
 716   }
 717 }
 718 
 719 // Walk the list of methods which might contain oops to the java heap.
 720 void CodeCache::scavenge_root_nmethods_do(CodeBlobToOopClosure* f) {
 721   assert_locked_or_safepoint(CodeCache_lock);
 722 
 723   const bool fix_relocations = f->fix_relocations();
 724   debug_only(mark_scavenge_root_nmethods());
 725 
 726   nmethod* prev = NULL;
 727   nmethod* cur = scavenge_root_nmethods();
 728   while (cur != NULL) {
 729     debug_only(cur->clear_scavenge_root_marked());
 730     assert(cur->scavenge_root_not_marked(), "");
 731     assert(cur->on_scavenge_root_list(), "else shouldn't be on this list");
 732 
 733     bool is_live = (!cur->is_zombie() && !cur->is_unloaded());
 734     LogTarget(Trace, gc, nmethod) lt;
 735     if (lt.is_enabled()) {
 736       LogStream ls(lt);
 737       CompileTask::print(&ls, cur,
 738         is_live ? "scavenge root " : "dead scavenge root", /*short_form:*/ true);
 739     }
 740     if (is_live) {
 741       // Perform cur->oops_do(f), maybe just once per nmethod.
 742       f->do_code_blob(cur);
 743     }
 744     nmethod* const next = cur->scavenge_root_link();
 745     // The scavengable nmethod list must contain all methods with scavengable
 746     // oops. It is safe to include more nmethod on the list, but we do not
 747     // expect any live non-scavengable nmethods on the list.
 748     if (fix_relocations) {
 749       if (!is_live || !cur->detect_scavenge_root_oops()) {
 750         unlink_scavenge_root_nmethod(cur, prev);
 751       } else {
 752         prev = cur;
 753       }
 754     }
 755     cur = next;
 756   }
 757 
 758   // Check for stray marks.
 759   debug_only(verify_perm_nmethods(NULL));
 760 }
 761 
 762 void CodeCache::register_scavenge_root_nmethod(nmethod* nm) {
 763   assert_locked_or_safepoint(CodeCache_lock);
 764   if (!nm->on_scavenge_root_list() && nm->detect_scavenge_root_oops()) {
 765     add_scavenge_root_nmethod(nm);
 766   }
 767 }
 768 
 769 void CodeCache::verify_scavenge_root_nmethod(nmethod* nm) {
 770   nm->verify_scavenge_root_oops();
 771 }
 772 
 773 void CodeCache::add_scavenge_root_nmethod(nmethod* nm) {
 774   assert_locked_or_safepoint(CodeCache_lock);
 775 
 776   nm->set_on_scavenge_root_list();
 777   nm->set_scavenge_root_link(_scavenge_root_nmethods);
 778   set_scavenge_root_nmethods(nm);
 779   print_trace("add_scavenge_root", nm);
 780 }
 781 
 782 void CodeCache::unlink_scavenge_root_nmethod(nmethod* nm, nmethod* prev) {
 783   assert_locked_or_safepoint(CodeCache_lock);
 784 
 785   assert((prev == NULL && scavenge_root_nmethods() == nm) ||
 786          (prev != NULL && prev->scavenge_root_link() == nm), "precondition");
 787 
 788   print_trace("unlink_scavenge_root", nm);
 789   if (prev == NULL) {
 790     set_scavenge_root_nmethods(nm->scavenge_root_link());
 791   } else {
 792     prev->set_scavenge_root_link(nm->scavenge_root_link());
 793   }
 794   nm->set_scavenge_root_link(NULL);
 795   nm->clear_on_scavenge_root_list();
 796 }
 797 
 798 void CodeCache::drop_scavenge_root_nmethod(nmethod* nm) {
 799   assert_locked_or_safepoint(CodeCache_lock);
 800 
 801   print_trace("drop_scavenge_root", nm);
 802   nmethod* prev = NULL;
 803   for (nmethod* cur = scavenge_root_nmethods(); cur != NULL; cur = cur->scavenge_root_link()) {
 804     if (cur == nm) {
 805       unlink_scavenge_root_nmethod(cur, prev);
 806       return;
 807     }
 808     prev = cur;
 809   }
 810   assert(false, "should have been on list");
 811 }
 812 
 813 void CodeCache::prune_scavenge_root_nmethods() {
 814   assert_locked_or_safepoint(CodeCache_lock);
 815 
 816   debug_only(mark_scavenge_root_nmethods());
 817 
 818   nmethod* last = NULL;
 819   nmethod* cur = scavenge_root_nmethods();
 820   while (cur != NULL) {
 821     nmethod* next = cur->scavenge_root_link();
 822     debug_only(cur->clear_scavenge_root_marked());
 823     assert(cur->scavenge_root_not_marked(), "");
 824     assert(cur->on_scavenge_root_list(), "else shouldn't be on this list");
 825 
 826     if (!cur->is_zombie() && !cur->is_unloaded()
 827         && cur->detect_scavenge_root_oops()) {
 828       // Keep it.  Advance 'last' to prevent deletion.
 829       last = cur;
 830     } else {
 831       // Prune it from the list, so we don't have to look at it any more.
 832       print_trace("prune_scavenge_root", cur);
 833       unlink_scavenge_root_nmethod(cur, last);
 834     }
 835     cur = next;
 836   }
 837 
 838   // Check for stray marks.
 839   debug_only(verify_perm_nmethods(NULL));
 840 }
 841 
 842 #ifndef PRODUCT
 843 void CodeCache::asserted_non_scavengable_nmethods_do(CodeBlobClosure* f) {
 844   // While we are here, verify the integrity of the list.
 845   mark_scavenge_root_nmethods();
 846   for (nmethod* cur = scavenge_root_nmethods(); cur != NULL; cur = cur->scavenge_root_link()) {
 847     assert(cur->on_scavenge_root_list(), "else shouldn't be on this list");
 848     cur->clear_scavenge_root_marked();
 849   }
 850   verify_perm_nmethods(f);
 851 }
 852 
 853 // Temporarily mark nmethods that are claimed to be on the scavenge list.
 854 void CodeCache::mark_scavenge_root_nmethods() {
 855   NMethodIterator iter;
 856   while(iter.next_alive()) {
 857     nmethod* nm = iter.method();
 858     assert(nm->scavenge_root_not_marked(), "clean state");
 859     if (nm->on_scavenge_root_list())
 860       nm->set_scavenge_root_marked();
 861   }
 862 }
 863 
 864 // If the closure is given, run it on the unlisted nmethods.
 865 // Also make sure that the effects of mark_scavenge_root_nmethods is gone.
 866 void CodeCache::verify_perm_nmethods(CodeBlobClosure* f_or_null) {
 867   NMethodIterator iter;
 868   while(iter.next_alive()) {
 869     nmethod* nm = iter.method();
 870     bool call_f = (f_or_null != NULL);
 871     assert(nm->scavenge_root_not_marked(), "must be already processed");
 872     if (nm->on_scavenge_root_list())
 873       call_f = false;  // don't show this one to the client
 874     Universe::heap()->verify_nmethod(nm);
 875     if (call_f)  f_or_null->do_code_blob(nm);
 876   }
 877 }
 878 #endif //PRODUCT
 879 
 880 void CodeCache::verify_clean_inline_caches() {
 881 #ifdef ASSERT
 882   NMethodIterator iter;
 883   while(iter.next_alive()) {
 884     nmethod* nm = iter.method();
 885     assert(!nm->is_unloaded(), "Tautology");
 886     nm->verify_clean_inline_caches();
 887     nm->verify();
 888   }
 889 #endif
 890 }
 891 
 892 void CodeCache::verify_icholder_relocations() {
 893 #ifdef ASSERT
 894   // make sure that we aren't leaking icholders
 895   int count = 0;
 896   FOR_ALL_HEAPS(heap) {
 897     FOR_ALL_BLOBS(cb, *heap) {
 898       CompiledMethod *nm = cb->as_compiled_method_or_null();
 899       if (nm != NULL) {
 900         count += nm->verify_icholder_relocations();
 901       }
 902     }
 903   }
 904   assert(count + InlineCacheBuffer::pending_icholder_count() + CompiledICHolder::live_not_claimed_count() ==
 905          CompiledICHolder::live_count(), "must agree");
 906 #endif
 907 }
 908 
 909 void CodeCache::gc_prologue() { }
 910 
 911 void CodeCache::gc_epilogue() {
 912   prune_scavenge_root_nmethods();
 913 }
 914 
 915 
 916 void CodeCache::do_unloading_nmethod_caches(bool class_unloading_occurred) {
 917   assert_locked_or_safepoint(CodeCache_lock);
 918   // Even if classes are not unloaded, there may have been some nmethods that are
 919   // unloaded because oops in them are no longer reachable.
 920   NOT_DEBUG(if (needs_cache_clean() || class_unloading_occurred)) {
 921     CompiledMethodIterator iter;
 922     while(iter.next_alive()) {
 923       CompiledMethod* cm = iter.method();
 924       assert(!cm->is_unloaded(), "Tautology");
 925       DEBUG_ONLY(if (needs_cache_clean() || class_unloading_occurred)) {
 926         // Clean up both unloaded klasses from nmethods and unloaded nmethods
 927         // from inline caches.
 928         cm->unload_nmethod_caches(/*parallel*/false, class_unloading_occurred);
 929       }
 930       DEBUG_ONLY(cm->verify());
 931       DEBUG_ONLY(cm->verify_oop_relocations());
 932     }
 933   }
 934 
 935   set_needs_cache_clean(false);
 936   verify_icholder_relocations();
 937 }
 938 
 939 void CodeCache::verify_oops() {
 940   MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 941   VerifyOopClosure voc;
 942   NMethodIterator iter;
 943   while(iter.next_alive()) {
 944     nmethod* nm = iter.method();
 945     nm->oops_do(&voc);
 946     nm->verify_oop_relocations();
 947   }
 948 }
 949 
 950 int CodeCache::blob_count(int code_blob_type) {
 951   CodeHeap* heap = get_code_heap(code_blob_type);
 952   return (heap != NULL) ? heap->blob_count() : 0;
 953 }
 954 
 955 int CodeCache::blob_count() {
 956   int count = 0;
 957   FOR_ALL_HEAPS(heap) {
 958     count += (*heap)->blob_count();
 959   }
 960   return count;
 961 }
 962 
 963 int CodeCache::nmethod_count(int code_blob_type) {
 964   CodeHeap* heap = get_code_heap(code_blob_type);
 965   return (heap != NULL) ? heap->nmethod_count() : 0;
 966 }
 967 
 968 int CodeCache::nmethod_count() {
 969   int count = 0;
 970   FOR_ALL_NMETHOD_HEAPS(heap) {
 971     count += (*heap)->nmethod_count();
 972   }
 973   return count;
 974 }
 975 
 976 int CodeCache::adapter_count(int code_blob_type) {
 977   CodeHeap* heap = get_code_heap(code_blob_type);
 978   return (heap != NULL) ? heap->adapter_count() : 0;
 979 }
 980 
 981 int CodeCache::adapter_count() {
 982   int count = 0;
 983   FOR_ALL_HEAPS(heap) {
 984     count += (*heap)->adapter_count();
 985   }
 986   return count;
 987 }
 988 
 989 address CodeCache::low_bound(int code_blob_type) {
 990   CodeHeap* heap = get_code_heap(code_blob_type);
 991   return (heap != NULL) ? (address)heap->low_boundary() : NULL;
 992 }
 993 
 994 address CodeCache::high_bound(int code_blob_type) {
 995   CodeHeap* heap = get_code_heap(code_blob_type);
 996   return (heap != NULL) ? (address)heap->high_boundary() : NULL;
 997 }
 998 
 999 size_t CodeCache::capacity() {
1000   size_t cap = 0;
1001   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1002     cap += (*heap)->capacity();
1003   }
1004   return cap;
1005 }
1006 
1007 size_t CodeCache::unallocated_capacity(int code_blob_type) {
1008   CodeHeap* heap = get_code_heap(code_blob_type);
1009   return (heap != NULL) ? heap->unallocated_capacity() : 0;
1010 }
1011 
1012 size_t CodeCache::unallocated_capacity() {
1013   size_t unallocated_cap = 0;
1014   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1015     unallocated_cap += (*heap)->unallocated_capacity();
1016   }
1017   return unallocated_cap;
1018 }
1019 
1020 size_t CodeCache::max_capacity() {
1021   size_t max_cap = 0;
1022   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1023     max_cap += (*heap)->max_capacity();
1024   }
1025   return max_cap;
1026 }
1027 
1028 /**
1029  * Returns the reverse free ratio. E.g., if 25% (1/4) of the code heap
1030  * is free, reverse_free_ratio() returns 4.
1031  */
1032 double CodeCache::reverse_free_ratio(int code_blob_type) {
1033   CodeHeap* heap = get_code_heap(code_blob_type);
1034   if (heap == NULL) {
1035     return 0;
1036   }
1037 
1038   double unallocated_capacity = MAX2((double)heap->unallocated_capacity(), 1.0); // Avoid division by 0;
1039   double max_capacity = (double)heap->max_capacity();
1040   double result = max_capacity / unallocated_capacity;
1041   assert (max_capacity >= unallocated_capacity, "Must be");
1042   assert (result >= 1.0, "reverse_free_ratio must be at least 1. It is %f", result);
1043   return result;
1044 }
1045 
1046 size_t CodeCache::bytes_allocated_in_freelists() {
1047   size_t allocated_bytes = 0;
1048   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1049     allocated_bytes += (*heap)->allocated_in_freelist();
1050   }
1051   return allocated_bytes;
1052 }
1053 
1054 int CodeCache::allocated_segments() {
1055   int number_of_segments = 0;
1056   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1057     number_of_segments += (*heap)->allocated_segments();
1058   }
1059   return number_of_segments;
1060 }
1061 
1062 size_t CodeCache::freelists_length() {
1063   size_t length = 0;
1064   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1065     length += (*heap)->freelist_length();
1066   }
1067   return length;
1068 }
1069 
1070 void icache_init();
1071 
1072 void CodeCache::initialize() {
1073   assert(CodeCacheSegmentSize >= (uintx)CodeEntryAlignment, "CodeCacheSegmentSize must be large enough to align entry points");
1074 #ifdef COMPILER2
1075   assert(CodeCacheSegmentSize >= (uintx)OptoLoopAlignment,  "CodeCacheSegmentSize must be large enough to align inner loops");
1076 #endif
1077   assert(CodeCacheSegmentSize >= sizeof(jdouble),    "CodeCacheSegmentSize must be large enough to align constants");
1078   // This was originally just a check of the alignment, causing failure, instead, round
1079   // the code cache to the page size.  In particular, Solaris is moving to a larger
1080   // default page size.
1081   CodeCacheExpansionSize = align_up(CodeCacheExpansionSize, os::vm_page_size());
1082 
1083   if (SegmentedCodeCache) {
1084     // Use multiple code heaps
1085     initialize_heaps();
1086   } else {
1087     // Use a single code heap
1088     FLAG_SET_ERGO(uintx, NonNMethodCodeHeapSize, 0);
1089     FLAG_SET_ERGO(uintx, ProfiledCodeHeapSize, 0);
1090     FLAG_SET_ERGO(uintx, NonProfiledCodeHeapSize, 0);
1091     ReservedCodeSpace rs = reserve_heap_memory(ReservedCodeCacheSize);
1092     add_heap(rs, "CodeCache", CodeBlobType::All);
1093   }
1094 
1095   // Initialize ICache flush mechanism
1096   // This service is needed for os::register_code_area
1097   icache_init();
1098 
1099   // Give OS a chance to register generated code area.
1100   // This is used on Windows 64 bit platforms to register
1101   // Structured Exception Handlers for our generated code.
1102   os::register_code_area((char*)low_bound(), (char*)high_bound());
1103 }
1104 
1105 void codeCache_init() {
1106   CodeCache::initialize();
1107   // Load AOT libraries and add AOT code heaps.
1108   AOTLoader::initialize();
1109 }
1110 
1111 //------------------------------------------------------------------------------------------------
1112 
1113 int CodeCache::number_of_nmethods_with_dependencies() {
1114   return _number_of_nmethods_with_dependencies;
1115 }
1116 
1117 void CodeCache::clear_inline_caches() {
1118   assert_locked_or_safepoint(CodeCache_lock);
1119   CompiledMethodIterator iter;
1120   while(iter.next_alive()) {
1121     iter.method()->clear_inline_caches();
1122   }
1123 }
1124 
1125 void CodeCache::cleanup_inline_caches() {
1126   assert_locked_or_safepoint(CodeCache_lock);
1127   NMethodIterator iter;
1128   while(iter.next_alive()) {
1129     iter.method()->cleanup_inline_caches(/*clean_all=*/true);
1130   }
1131 }
1132 
1133 // Keeps track of time spent for checking dependencies
1134 NOT_PRODUCT(static elapsedTimer dependentCheckTime;)
1135 
1136 int CodeCache::mark_for_deoptimization(KlassDepChange& changes) {
1137   MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1138   int number_of_marked_CodeBlobs = 0;
1139 
1140   // search the hierarchy looking for nmethods which are affected by the loading of this class
1141 
1142   // then search the interfaces this class implements looking for nmethods
1143   // which might be dependent of the fact that an interface only had one
1144   // implementor.
1145   // nmethod::check_all_dependencies works only correctly, if no safepoint
1146   // can happen
1147   NoSafepointVerifier nsv;
1148   for (DepChange::ContextStream str(changes, nsv); str.next(); ) {
1149     Klass* d = str.klass();
1150     number_of_marked_CodeBlobs += InstanceKlass::cast(d)->mark_dependent_nmethods(changes);
1151   }
1152 
1153 #ifndef PRODUCT
1154   if (VerifyDependencies) {
1155     // Object pointers are used as unique identifiers for dependency arguments. This
1156     // is only possible if no safepoint, i.e., GC occurs during the verification code.
1157     dependentCheckTime.start();
1158     nmethod::check_all_dependencies(changes);
1159     dependentCheckTime.stop();
1160   }
1161 #endif
1162 
1163   return number_of_marked_CodeBlobs;
1164 }
1165 
1166 CompiledMethod* CodeCache::find_compiled(void* start) {
1167   CodeBlob *cb = find_blob(start);
1168   assert(cb == NULL || cb->is_compiled(), "did not find an compiled_method");
1169   return (CompiledMethod*)cb;
1170 }
1171 
1172 bool CodeCache::is_far_target(address target) {
1173 #if INCLUDE_AOT
1174   return NativeCall::is_far_call(_low_bound,  target) ||
1175          NativeCall::is_far_call(_high_bound, target);
1176 #else
1177   return false;
1178 #endif
1179 }
1180 
1181 #ifdef HOTSWAP
1182 int CodeCache::mark_for_evol_deoptimization(InstanceKlass* dependee) {
1183   MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1184   int number_of_marked_CodeBlobs = 0;
1185 
1186   // Deoptimize all methods of the evolving class itself
1187   Array<Method*>* old_methods = dependee->methods();
1188   for (int i = 0; i < old_methods->length(); i++) {
1189     ResourceMark rm;
1190     Method* old_method = old_methods->at(i);
1191     CompiledMethod* nm = old_method->code();
1192     if (nm != NULL) {
1193       nm->mark_for_deoptimization();
1194       number_of_marked_CodeBlobs++;
1195     }
1196   }
1197 
1198   CompiledMethodIterator iter;
1199   while(iter.next_alive()) {
1200     CompiledMethod* nm = iter.method();
1201     if (nm->is_marked_for_deoptimization()) {
1202       // ...Already marked in the previous pass; don't count it again.
1203     } else if (nm->is_evol_dependent_on(dependee)) {
1204       ResourceMark rm;
1205       nm->mark_for_deoptimization();
1206       number_of_marked_CodeBlobs++;
1207     } else  {
1208       // flush caches in case they refer to a redefined Method*
1209       nm->clear_inline_caches();
1210     }
1211   }
1212 
1213   return number_of_marked_CodeBlobs;
1214 }
1215 #endif // HOTSWAP
1216 
1217 
1218 // Deoptimize all methods
1219 void CodeCache::mark_all_nmethods_for_deoptimization() {
1220   MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1221   CompiledMethodIterator iter;
1222   while(iter.next_alive()) {
1223     CompiledMethod* nm = iter.method();
1224     if (!nm->method()->is_method_handle_intrinsic()) {
1225       nm->mark_for_deoptimization();
1226     }
1227   }
1228 }
1229 
1230 int CodeCache::mark_for_deoptimization(Method* dependee) {
1231   MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1232   int number_of_marked_CodeBlobs = 0;
1233 
1234   CompiledMethodIterator iter;
1235   while(iter.next_alive()) {
1236     CompiledMethod* nm = iter.method();
1237     if (nm->is_dependent_on_method(dependee)) {
1238       ResourceMark rm;
1239       nm->mark_for_deoptimization();
1240       number_of_marked_CodeBlobs++;
1241     }
1242   }
1243 
1244   return number_of_marked_CodeBlobs;
1245 }
1246 
1247 void CodeCache::make_marked_nmethods_not_entrant() {
1248   assert_locked_or_safepoint(CodeCache_lock);
1249   CompiledMethodIterator iter;
1250   while(iter.next_alive()) {
1251     CompiledMethod* nm = iter.method();
1252     if (nm->is_marked_for_deoptimization() && !nm->is_not_entrant()) {
1253       nm->make_not_entrant();
1254     }
1255   }
1256 }
1257 
1258 // Flushes compiled methods dependent on dependee.
1259 void CodeCache::flush_dependents_on(InstanceKlass* dependee) {
1260   assert_lock_strong(Compile_lock);
1261 
1262   if (number_of_nmethods_with_dependencies() == 0) return;
1263 
1264   // CodeCache can only be updated by a thread_in_VM and they will all be
1265   // stopped during the safepoint so CodeCache will be safe to update without
1266   // holding the CodeCache_lock.
1267 
1268   KlassDepChange changes(dependee);
1269 
1270   // Compute the dependent nmethods
1271   if (mark_for_deoptimization(changes) > 0) {
1272     // At least one nmethod has been marked for deoptimization
1273     VM_Deoptimize op;
1274     VMThread::execute(&op);
1275   }
1276 }
1277 
1278 #ifdef HOTSWAP
1279 // Flushes compiled methods dependent on dependee in the evolutionary sense
1280 void CodeCache::flush_evol_dependents_on(InstanceKlass* ev_k) {
1281   // --- Compile_lock is not held. However we are at a safepoint.
1282   assert_locked_or_safepoint(Compile_lock);
1283   if (number_of_nmethods_with_dependencies() == 0 && !UseAOT) return;
1284 
1285   // CodeCache can only be updated by a thread_in_VM and they will all be
1286   // stopped during the safepoint so CodeCache will be safe to update without
1287   // holding the CodeCache_lock.
1288 
1289   // Compute the dependent nmethods
1290   if (mark_for_evol_deoptimization(ev_k) > 0) {
1291     // At least one nmethod has been marked for deoptimization
1292 
1293     // All this already happens inside a VM_Operation, so we'll do all the work here.
1294     // Stuff copied from VM_Deoptimize and modified slightly.
1295 
1296     // We do not want any GCs to happen while we are in the middle of this VM operation
1297     ResourceMark rm;
1298     DeoptimizationMarker dm;
1299 
1300     // Deoptimize all activations depending on marked nmethods
1301     Deoptimization::deoptimize_dependents();
1302 
1303     // Make the dependent methods not entrant
1304     make_marked_nmethods_not_entrant();
1305   }
1306 }
1307 #endif // HOTSWAP
1308 
1309 
1310 // Flushes compiled methods dependent on dependee
1311 void CodeCache::flush_dependents_on_method(const methodHandle& m_h) {
1312   // --- Compile_lock is not held. However we are at a safepoint.
1313   assert_locked_or_safepoint(Compile_lock);
1314 
1315   // CodeCache can only be updated by a thread_in_VM and they will all be
1316   // stopped dring the safepoint so CodeCache will be safe to update without
1317   // holding the CodeCache_lock.
1318 
1319   // Compute the dependent nmethods
1320   if (mark_for_deoptimization(m_h()) > 0) {
1321     // At least one nmethod has been marked for deoptimization
1322 
1323     // All this already happens inside a VM_Operation, so we'll do all the work here.
1324     // Stuff copied from VM_Deoptimize and modified slightly.
1325 
1326     // We do not want any GCs to happen while we are in the middle of this VM operation
1327     ResourceMark rm;
1328     DeoptimizationMarker dm;
1329 
1330     // Deoptimize all activations depending on marked nmethods
1331     Deoptimization::deoptimize_dependents();
1332 
1333     // Make the dependent methods not entrant
1334     make_marked_nmethods_not_entrant();
1335   }
1336 }
1337 
1338 void CodeCache::verify() {
1339   assert_locked_or_safepoint(CodeCache_lock);
1340   FOR_ALL_HEAPS(heap) {
1341     (*heap)->verify();
1342     FOR_ALL_BLOBS(cb, *heap) {
1343       if (cb->is_alive()) {
1344         cb->verify();
1345       }
1346     }
1347   }
1348 }
1349 
1350 // A CodeHeap is full. Print out warning and report event.
1351 PRAGMA_DIAG_PUSH
1352 PRAGMA_FORMAT_NONLITERAL_IGNORED
1353 void CodeCache::report_codemem_full(int code_blob_type, bool print) {
1354   // Get nmethod heap for the given CodeBlobType and build CodeCacheFull event
1355   CodeHeap* heap = get_code_heap(code_blob_type);
1356   assert(heap != NULL, "heap is null");
1357 
1358   if ((heap->full_count() == 0) || print) {
1359     // Not yet reported for this heap, report
1360     if (SegmentedCodeCache) {
1361       ResourceMark rm;
1362       stringStream msg1_stream, msg2_stream;
1363       msg1_stream.print("%s is full. Compiler has been disabled.",
1364                         get_code_heap_name(code_blob_type));
1365       msg2_stream.print("Try increasing the code heap size using -XX:%s=",
1366                  get_code_heap_flag_name(code_blob_type));
1367       const char *msg1 = msg1_stream.as_string();
1368       const char *msg2 = msg2_stream.as_string();
1369 
1370       log_warning(codecache)("%s", msg1);
1371       log_warning(codecache)("%s", msg2);
1372       warning("%s", msg1);
1373       warning("%s", msg2);
1374     } else {
1375       const char *msg1 = "CodeCache is full. Compiler has been disabled.";
1376       const char *msg2 = "Try increasing the code cache size using -XX:ReservedCodeCacheSize=";
1377 
1378       log_warning(codecache)("%s", msg1);
1379       log_warning(codecache)("%s", msg2);
1380       warning("%s", msg1);
1381       warning("%s", msg2);
1382     }
1383     ResourceMark rm;
1384     stringStream s;
1385     // Dump code cache into a buffer before locking the tty.
1386     {
1387       MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1388       print_summary(&s);
1389     }
1390     {
1391       ttyLocker ttyl;
1392       tty->print("%s", s.as_string());
1393     }
1394 
1395     if (heap->full_count() == 0) {
1396       if (PrintCodeHeapAnalytics) {
1397         CompileBroker::print_heapinfo(tty, "all", 4096); // details, may be a lot!
1398       }
1399     }
1400   }
1401 
1402   heap->report_full();
1403 
1404   EventCodeCacheFull event;
1405   if (event.should_commit()) {
1406     event.set_codeBlobType((u1)code_blob_type);
1407     event.set_startAddress((u8)heap->low_boundary());
1408     event.set_commitedTopAddress((u8)heap->high());
1409     event.set_reservedTopAddress((u8)heap->high_boundary());
1410     event.set_entryCount(heap->blob_count());
1411     event.set_methodCount(heap->nmethod_count());
1412     event.set_adaptorCount(heap->adapter_count());
1413     event.set_unallocatedCapacity(heap->unallocated_capacity());
1414     event.set_fullCount(heap->full_count());
1415     event.commit();
1416   }
1417 }
1418 PRAGMA_DIAG_POP
1419 
1420 void CodeCache::print_memory_overhead() {
1421   size_t wasted_bytes = 0;
1422   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1423       CodeHeap* curr_heap = *heap;
1424       for (CodeBlob* cb = (CodeBlob*)curr_heap->first(); cb != NULL; cb = (CodeBlob*)curr_heap->next(cb)) {
1425         HeapBlock* heap_block = ((HeapBlock*)cb) - 1;
1426         wasted_bytes += heap_block->length() * CodeCacheSegmentSize - cb->size();
1427       }
1428   }
1429   // Print bytes that are allocated in the freelist
1430   ttyLocker ttl;
1431   tty->print_cr("Number of elements in freelist: " SSIZE_FORMAT,       freelists_length());
1432   tty->print_cr("Allocated in freelist:          " SSIZE_FORMAT "kB",  bytes_allocated_in_freelists()/K);
1433   tty->print_cr("Unused bytes in CodeBlobs:      " SSIZE_FORMAT "kB",  (wasted_bytes/K));
1434   tty->print_cr("Segment map size:               " SSIZE_FORMAT "kB",  allocated_segments()/K); // 1 byte per segment
1435 }
1436 
1437 //------------------------------------------------------------------------------------------------
1438 // Non-product version
1439 
1440 #ifndef PRODUCT
1441 
1442 void CodeCache::print_trace(const char* event, CodeBlob* cb, int size) {
1443   if (PrintCodeCache2) {  // Need to add a new flag
1444     ResourceMark rm;
1445     if (size == 0)  size = cb->size();
1446     tty->print_cr("CodeCache %s:  addr: " INTPTR_FORMAT ", size: 0x%x", event, p2i(cb), size);
1447   }
1448 }
1449 
1450 void CodeCache::print_internals() {
1451   int nmethodCount = 0;
1452   int runtimeStubCount = 0;
1453   int adapterCount = 0;
1454   int deoptimizationStubCount = 0;
1455   int uncommonTrapStubCount = 0;
1456   int bufferBlobCount = 0;
1457   int total = 0;
1458   int nmethodAlive = 0;
1459   int nmethodNotEntrant = 0;
1460   int nmethodZombie = 0;
1461   int nmethodUnloaded = 0;
1462   int nmethodJava = 0;
1463   int nmethodNative = 0;
1464   int max_nm_size = 0;
1465   ResourceMark rm;
1466 
1467   int i = 0;
1468   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1469     if ((_nmethod_heaps->length() >= 1) && Verbose) {
1470       tty->print_cr("-- %s --", (*heap)->name());
1471     }
1472     FOR_ALL_BLOBS(cb, *heap) {
1473       total++;
1474       if (cb->is_nmethod()) {
1475         nmethod* nm = (nmethod*)cb;
1476 
1477         if (Verbose && nm->method() != NULL) {
1478           ResourceMark rm;
1479           char *method_name = nm->method()->name_and_sig_as_C_string();
1480           tty->print("%s", method_name);
1481           if(nm->is_alive()) { tty->print_cr(" alive"); }
1482           if(nm->is_not_entrant()) { tty->print_cr(" not-entrant"); }
1483           if(nm->is_zombie()) { tty->print_cr(" zombie"); }
1484         }
1485 
1486         nmethodCount++;
1487 
1488         if(nm->is_alive()) { nmethodAlive++; }
1489         if(nm->is_not_entrant()) { nmethodNotEntrant++; }
1490         if(nm->is_zombie()) { nmethodZombie++; }
1491         if(nm->is_unloaded()) { nmethodUnloaded++; }
1492         if(nm->method() != NULL && nm->is_native_method()) { nmethodNative++; }
1493 
1494         if(nm->method() != NULL && nm->is_java_method()) {
1495           nmethodJava++;
1496           max_nm_size = MAX2(max_nm_size, nm->size());
1497         }
1498       } else if (cb->is_runtime_stub()) {
1499         runtimeStubCount++;
1500       } else if (cb->is_deoptimization_stub()) {
1501         deoptimizationStubCount++;
1502       } else if (cb->is_uncommon_trap_stub()) {
1503         uncommonTrapStubCount++;
1504       } else if (cb->is_adapter_blob()) {
1505         adapterCount++;
1506       } else if (cb->is_buffer_blob()) {
1507         bufferBlobCount++;
1508       }
1509     }
1510   }
1511 
1512   int bucketSize = 512;
1513   int bucketLimit = max_nm_size / bucketSize + 1;
1514   int *buckets = NEW_C_HEAP_ARRAY(int, bucketLimit, mtCode);
1515   memset(buckets, 0, sizeof(int) * bucketLimit);
1516 
1517   NMethodIterator iter;
1518   while(iter.next()) {
1519     nmethod* nm = iter.method();
1520     if(nm->method() != NULL && nm->is_java_method()) {
1521       buckets[nm->size() / bucketSize]++;
1522     }
1523   }
1524 
1525   tty->print_cr("Code Cache Entries (total of %d)",total);
1526   tty->print_cr("-------------------------------------------------");
1527   tty->print_cr("nmethods: %d",nmethodCount);
1528   tty->print_cr("\talive: %d",nmethodAlive);
1529   tty->print_cr("\tnot_entrant: %d",nmethodNotEntrant);
1530   tty->print_cr("\tzombie: %d",nmethodZombie);
1531   tty->print_cr("\tunloaded: %d",nmethodUnloaded);
1532   tty->print_cr("\tjava: %d",nmethodJava);
1533   tty->print_cr("\tnative: %d",nmethodNative);
1534   tty->print_cr("runtime_stubs: %d",runtimeStubCount);
1535   tty->print_cr("adapters: %d",adapterCount);
1536   tty->print_cr("buffer blobs: %d",bufferBlobCount);
1537   tty->print_cr("deoptimization_stubs: %d",deoptimizationStubCount);
1538   tty->print_cr("uncommon_traps: %d",uncommonTrapStubCount);
1539   tty->print_cr("\nnmethod size distribution (non-zombie java)");
1540   tty->print_cr("-------------------------------------------------");
1541 
1542   for(int i=0; i<bucketLimit; i++) {
1543     if(buckets[i] != 0) {
1544       tty->print("%d - %d bytes",i*bucketSize,(i+1)*bucketSize);
1545       tty->fill_to(40);
1546       tty->print_cr("%d",buckets[i]);
1547     }
1548   }
1549 
1550   FREE_C_HEAP_ARRAY(int, buckets);
1551   print_memory_overhead();
1552 }
1553 
1554 #endif // !PRODUCT
1555 
1556 void CodeCache::print() {
1557   print_summary(tty);
1558 
1559 #ifndef PRODUCT
1560   if (!Verbose) return;
1561 
1562   CodeBlob_sizes live;
1563   CodeBlob_sizes dead;
1564 
1565   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1566     FOR_ALL_BLOBS(cb, *heap) {
1567       if (!cb->is_alive()) {
1568         dead.add(cb);
1569       } else {
1570         live.add(cb);
1571       }
1572     }
1573   }
1574 
1575   tty->print_cr("CodeCache:");
1576   tty->print_cr("nmethod dependency checking time %fs", dependentCheckTime.seconds());
1577 
1578   if (!live.is_empty()) {
1579     live.print("live");
1580   }
1581   if (!dead.is_empty()) {
1582     dead.print("dead");
1583   }
1584 
1585   if (WizardMode) {
1586      // print the oop_map usage
1587     int code_size = 0;
1588     int number_of_blobs = 0;
1589     int number_of_oop_maps = 0;
1590     int map_size = 0;
1591     FOR_ALL_ALLOCABLE_HEAPS(heap) {
1592       FOR_ALL_BLOBS(cb, *heap) {
1593         if (cb->is_alive()) {
1594           number_of_blobs++;
1595           code_size += cb->code_size();
1596           ImmutableOopMapSet* set = cb->oop_maps();
1597           if (set != NULL) {
1598             number_of_oop_maps += set->count();
1599             map_size           += set->nr_of_bytes();
1600           }
1601         }
1602       }
1603     }
1604     tty->print_cr("OopMaps");
1605     tty->print_cr("  #blobs    = %d", number_of_blobs);
1606     tty->print_cr("  code size = %d", code_size);
1607     tty->print_cr("  #oop_maps = %d", number_of_oop_maps);
1608     tty->print_cr("  map size  = %d", map_size);
1609   }
1610 
1611 #endif // !PRODUCT
1612 }
1613 
1614 void CodeCache::print_summary(outputStream* st, bool detailed) {
1615   int full_count = 0;
1616   FOR_ALL_HEAPS(heap_iterator) {
1617     CodeHeap* heap = (*heap_iterator);
1618     size_t total = (heap->high_boundary() - heap->low_boundary());
1619     if (_heaps->length() >= 1) {
1620       st->print("%s:", heap->name());
1621     } else {
1622       st->print("CodeCache:");
1623     }
1624     st->print_cr(" size=" SIZE_FORMAT "Kb used=" SIZE_FORMAT
1625                  "Kb max_used=" SIZE_FORMAT "Kb free=" SIZE_FORMAT "Kb",
1626                  total/K, (total - heap->unallocated_capacity())/K,
1627                  heap->max_allocated_capacity()/K, heap->unallocated_capacity()/K);
1628 
1629     if (detailed) {
1630       st->print_cr(" bounds [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT "]",
1631                    p2i(heap->low_boundary()),
1632                    p2i(heap->high()),
1633                    p2i(heap->high_boundary()));
1634 
1635       full_count += get_codemem_full_count(heap->code_blob_type());
1636     }
1637   }
1638 
1639   if (detailed) {
1640     st->print_cr(" total_blobs=" UINT32_FORMAT " nmethods=" UINT32_FORMAT
1641                        " adapters=" UINT32_FORMAT,
1642                        blob_count(), nmethod_count(), adapter_count());
1643     st->print_cr(" compilation: %s", CompileBroker::should_compile_new_jobs() ?
1644                  "enabled" : Arguments::mode() == Arguments::_int ?
1645                  "disabled (interpreter mode)" :
1646                  "disabled (not enough contiguous free space left)");
1647     st->print_cr("              stopped_count=%d, restarted_count=%d",
1648                  CompileBroker::get_total_compiler_stopped_count(),
1649                  CompileBroker::get_total_compiler_restarted_count());
1650     st->print_cr(" full_count=%d", full_count);
1651   }
1652 }
1653 
1654 void CodeCache::print_codelist(outputStream* st) {
1655   MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1656 
1657   CompiledMethodIterator iter;
1658   while (iter.next_alive()) {
1659     CompiledMethod* cm = iter.method();
1660     ResourceMark rm;
1661     char* method_name = cm->method()->name_and_sig_as_C_string();
1662     st->print_cr("%d %d %d %s [" INTPTR_FORMAT ", " INTPTR_FORMAT " - " INTPTR_FORMAT "]",
1663                  cm->compile_id(), cm->comp_level(), cm->get_state(),
1664                  method_name,
1665                  (intptr_t)cm->header_begin(), (intptr_t)cm->code_begin(), (intptr_t)cm->code_end());
1666   }
1667 }
1668 
1669 void CodeCache::print_layout(outputStream* st) {
1670   MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1671   ResourceMark rm;
1672   print_summary(st, true);
1673 }
1674 
1675 void CodeCache::log_state(outputStream* st) {
1676   st->print(" total_blobs='" UINT32_FORMAT "' nmethods='" UINT32_FORMAT "'"
1677             " adapters='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'",
1678             blob_count(), nmethod_count(), adapter_count(),
1679             unallocated_capacity());
1680 }
1681 
1682 //---<  BEGIN  >--- CodeHeap State Analytics.
1683 
1684 void CodeCache::aggregate(outputStream *out, size_t granularity) {
1685   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1686     CodeHeapState::aggregate(out, (*heap), granularity);
1687   }
1688 }
1689 
1690 void CodeCache::discard(outputStream *out) {
1691   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1692     CodeHeapState::discard(out, (*heap));
1693   }
1694 }
1695 
1696 void CodeCache::print_usedSpace(outputStream *out) {
1697   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1698     CodeHeapState::print_usedSpace(out, (*heap));
1699   }
1700 }
1701 
1702 void CodeCache::print_freeSpace(outputStream *out) {
1703   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1704     CodeHeapState::print_freeSpace(out, (*heap));
1705   }
1706 }
1707 
1708 void CodeCache::print_count(outputStream *out) {
1709   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1710     CodeHeapState::print_count(out, (*heap));
1711   }
1712 }
1713 
1714 void CodeCache::print_space(outputStream *out) {
1715   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1716     CodeHeapState::print_space(out, (*heap));
1717   }
1718 }
1719 
1720 void CodeCache::print_age(outputStream *out) {
1721   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1722     CodeHeapState::print_age(out, (*heap));
1723   }
1724 }
1725 
1726 void CodeCache::print_names(outputStream *out) {
1727   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1728     CodeHeapState::print_names(out, (*heap));
1729   }
1730 }
1731 //---<  END  >--- CodeHeap State Analytics.