< prev index next >

src/hotspot/share/code/codeCache.cpp

Print this page

   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 

  25 #include "code/codeBlob.hpp"
  26 #include "code/codeCache.hpp"
  27 #include "code/codeHeapState.hpp"
  28 #include "code/compiledIC.hpp"
  29 #include "code/dependencies.hpp"
  30 #include "code/dependencyContext.hpp"
  31 #include "code/nmethod.hpp"
  32 #include "code/pcDesc.hpp"
  33 #include "compiler/compilationPolicy.hpp"
  34 #include "compiler/compileBroker.hpp"
  35 #include "compiler/compilerDefinitions.inline.hpp"
  36 #include "compiler/oopMap.hpp"
  37 #include "gc/shared/barrierSetNMethod.hpp"
  38 #include "gc/shared/classUnloadingContext.hpp"
  39 #include "gc/shared/collectedHeap.hpp"
  40 #include "jfr/jfrEvents.hpp"
  41 #include "jvm_io.h"
  42 #include "logging/log.hpp"
  43 #include "logging/logStream.hpp"
  44 #include "memory/allocation.inline.hpp"

 153       scopes_data_size += nm->scopes_data_size();
 154       scopes_pcs_size  += nm->scopes_pcs_size();
 155     } else {
 156       code_size        += cb->code_size();
 157     }
 158   }
 159 };
 160 
 161 // Iterate over all CodeHeaps
 162 #define FOR_ALL_HEAPS(heap) for (GrowableArrayIterator<CodeHeap*> heap = _heaps->begin(); heap != _heaps->end(); ++heap)
 163 #define FOR_ALL_ALLOCABLE_HEAPS(heap) for (GrowableArrayIterator<CodeHeap*> heap = _allocable_heaps->begin(); heap != _allocable_heaps->end(); ++heap)
 164 
 165 // Iterate over all CodeBlobs (cb) on the given CodeHeap
 166 #define FOR_ALL_BLOBS(cb, heap) for (CodeBlob* cb = first_blob(heap); cb != nullptr; cb = next_blob(heap, cb))
 167 
 168 address CodeCache::_low_bound = nullptr;
 169 address CodeCache::_high_bound = nullptr;
 170 volatile int CodeCache::_number_of_nmethods_with_dependencies = 0;
 171 ExceptionCache* volatile CodeCache::_exception_cache_purge_list = nullptr;
 172 


 173 // Initialize arrays of CodeHeap subsets
 174 GrowableArray<CodeHeap*>* CodeCache::_heaps = new(mtCode) GrowableArray<CodeHeap*> (static_cast<int>(CodeBlobType::All), mtCode);
 175 GrowableArray<CodeHeap*>* CodeCache::_nmethod_heaps = new(mtCode) GrowableArray<CodeHeap*> (static_cast<int>(CodeBlobType::All), mtCode);
 176 GrowableArray<CodeHeap*>* CodeCache::_allocable_heaps = new(mtCode) GrowableArray<CodeHeap*> (static_cast<int>(CodeBlobType::All), mtCode);
 177 
 178 static void check_min_size(const char* codeheap, size_t size, size_t required_size) {
 179   if (size < required_size) {
 180     log_debug(codecache)("Code heap (%s) size %zuK below required minimal size %zuK",
 181                          codeheap, size/K, required_size/K);
 182     err_msg title("Not enough space in %s to run VM", codeheap);
 183     err_msg message("%zuK < %zuK", size/K, required_size/K);
 184     vm_exit_during_initialization(title, message);
 185   }
 186 }
 187 
 188 struct CodeHeapInfo {
 189   size_t size;
 190   bool set;
 191   bool enabled;
 192 };
 193 
 194 static void set_size_of_unset_code_heap(CodeHeapInfo* heap, size_t available_size, size_t used_size, size_t min_size) {
 195   assert(!heap->set, "sanity");
 196   heap->size = (available_size > (used_size + min_size)) ? (available_size - used_size) : min_size;
 197 }
 198 
 199 void CodeCache::initialize_heaps() {
 200 
 201   CodeHeapInfo non_nmethod = {NonNMethodCodeHeapSize, FLAG_IS_CMDLINE(NonNMethodCodeHeapSize), true};
 202   CodeHeapInfo profiled = {ProfiledCodeHeapSize, FLAG_IS_CMDLINE(ProfiledCodeHeapSize), true};
 203   CodeHeapInfo non_profiled = {NonProfiledCodeHeapSize, FLAG_IS_CMDLINE(NonProfiledCodeHeapSize), true};
 204 
 205   const bool cache_size_set   = FLAG_IS_CMDLINE(ReservedCodeCacheSize);
 206   const size_t ps             = page_size(false, 8);
 207   const size_t min_size       = MAX2(os::vm_allocation_granularity(), ps);
 208   const size_t min_cache_size = CodeCacheMinimumUseSpace DEBUG_ONLY(* 3); // Make sure we have enough space for VM internal code
 209   size_t cache_size           = align_up(ReservedCodeCacheSize, min_size);
 210 
 211   // Prerequisites
 212   if (!heap_available(CodeBlobType::MethodProfiled)) {
 213     // For compatibility reasons, disabled tiered compilation overrides
 214     // segment size even if it is set explicitly.
 215     non_profiled.size += profiled.size;
 216     // Profiled code heap is not available, forcibly set size to 0
 217     profiled.size = 0;
 218     profiled.set = true;
 219     profiled.enabled = false;
 220   }
 221 
 222   assert(heap_available(CodeBlobType::MethodNonProfiled), "MethodNonProfiled heap is always available for segmented code heap");
 223 
 224   size_t compiler_buffer_size = 0;
 225   COMPILER1_PRESENT(compiler_buffer_size += CompilationPolicy::c1_count() * Compiler::code_buffer_size());
 226   COMPILER2_PRESENT(compiler_buffer_size += CompilationPolicy::c2_count() * C2Compiler::initial_code_buffer_size());

 227 
 228   if (!non_nmethod.set) {
 229     non_nmethod.size += compiler_buffer_size;
 230     // Further down, just before FLAG_SET_ERGO(), all segment sizes are
 231     // aligned down to the next lower multiple of min_size. For large page
 232     // sizes, this may result in (non_nmethod.size == 0) which is not acceptable.
 233     // Therefore, force non_nmethod.size to at least min_size.
 234     non_nmethod.size = MAX2(non_nmethod.size, min_size);
 235   }
 236 
 237   if (!profiled.set && !non_profiled.set) {
 238     non_profiled.size = profiled.size = (cache_size > non_nmethod.size + 2 * min_size) ?
 239                                         (cache_size - non_nmethod.size) / 2 : min_size;
 240   }
 241 
 242   if (profiled.set && !non_profiled.set) {
 243     set_size_of_unset_code_heap(&non_profiled, cache_size, non_nmethod.size + profiled.size, min_size);
 244   }
 245 
 246   if (!profiled.set && non_profiled.set) {

 301     if (ps < lg_ps) {
 302       log_warning(codecache)("Code cache size too small for " PROPERFMT " pages. "
 303                              "Reverting to smaller page size (" PROPERFMT ").",
 304                              PROPERFMTARGS(lg_ps), PROPERFMTARGS(ps));
 305     }
 306   }
 307 
 308   // Note: if large page support is enabled, min_size is at least the large
 309   // page size. This ensures that the code cache is covered by large pages.
 310   non_profiled.size += non_nmethod.size & alignment_mask(min_size);
 311   non_profiled.size += profiled.size & alignment_mask(min_size);
 312   non_nmethod.size = align_down(non_nmethod.size, min_size);
 313   profiled.size = align_down(profiled.size, min_size);
 314   non_profiled.size = align_down(non_profiled.size, min_size);
 315 
 316   FLAG_SET_ERGO(NonNMethodCodeHeapSize, non_nmethod.size);
 317   FLAG_SET_ERGO(ProfiledCodeHeapSize, profiled.size);
 318   FLAG_SET_ERGO(NonProfiledCodeHeapSize, non_profiled.size);
 319   FLAG_SET_ERGO(ReservedCodeCacheSize, cache_size);
 320 



 321   ReservedSpace rs = reserve_heap_memory(cache_size, ps);
 322 
 323   // Register CodeHeaps with LSan as we sometimes embed pointers to malloc memory.
 324   LSAN_REGISTER_ROOT_REGION(rs.base(), rs.size());
 325 
 326   size_t offset = 0;






 327   if (profiled.enabled) {
 328     ReservedSpace profiled_space = rs.partition(offset, profiled.size);
 329     offset += profiled.size;
 330     // Tier 2 and tier 3 (profiled) methods
 331     add_heap(profiled_space, "CodeHeap 'profiled nmethods'", CodeBlobType::MethodProfiled);
 332   }
 333 
 334   ReservedSpace non_method_space = rs.partition(offset, non_nmethod.size);
 335   offset += non_nmethod.size;
 336   // Non-nmethods (stubs, adapters, ...)
 337   add_heap(non_method_space, "CodeHeap 'non-nmethods'", CodeBlobType::NonNMethod);
 338 
 339   if (non_profiled.enabled) {
 340     ReservedSpace non_profiled_space  = rs.partition(offset, non_profiled.size);
 341     // Tier 1 and tier 4 (non-profiled) methods and native methods
 342     add_heap(non_profiled_space, "CodeHeap 'non-profiled nmethods'", CodeBlobType::MethodNonProfiled);
 343   }
 344 }
 345 








 346 size_t CodeCache::page_size(bool aligned, size_t min_pages) {
 347   return aligned ? os::page_size_for_region_aligned(ReservedCodeCacheSize, min_pages) :
 348                    os::page_size_for_region_unaligned(ReservedCodeCacheSize, min_pages);
 349 }
 350 
 351 ReservedSpace CodeCache::reserve_heap_memory(size_t size, size_t rs_ps) {
 352   // Align and reserve space for code cache
 353   const size_t rs_align = MAX2(rs_ps, os::vm_allocation_granularity());
 354   const size_t rs_size = align_up(size, rs_align);
 355 
 356   ReservedSpace rs = CodeMemoryReserver::reserve(rs_size, rs_align, rs_ps);
 357   if (!rs.is_reserved()) {
 358     vm_exit_during_initialization(err_msg("Could not reserve enough space for code cache (%zuK)",
 359                                           rs_size/K));
 360   }
 361 
 362   // Initialize bounds
 363   _low_bound = (address)rs.base();
 364   _high_bound = _low_bound + rs.size();
 365   return rs;

1188                             AnyObj::RESOURCE_AREA, mtInternal,
1189                             &DependencySignature::hash,
1190                             &DependencySignature::equals> DepTable;
1191 
1192   DepTable* table = new DepTable();
1193 
1194   // Iterate over live nmethods and check dependencies of all nmethods that are not
1195   // marked for deoptimization. A particular dependency is only checked once.
1196   NMethodIterator iter(NMethodIterator::not_unloading);
1197   while(iter.next()) {
1198     nmethod* nm = iter.method();
1199     // Only notify for live nmethods
1200     if (!nm->is_marked_for_deoptimization()) {
1201       for (Dependencies::DepStream deps(nm); deps.next(); ) {
1202         // Construct abstraction of a dependency.
1203         DependencySignature* current_sig = new DependencySignature(deps);
1204 
1205         // Determine if dependency is already checked. table->put(...) returns
1206         // 'true' if the dependency is added (i.e., was not in the hashtable).
1207         if (table->put(*current_sig, 1)) {
1208           if (deps.check_dependency() != nullptr) {

1209             // Dependency checking failed. Print out information about the failed
1210             // dependency and finally fail with an assert. We can fail here, since
1211             // dependency checking is never done in a product build.
1212             tty->print_cr("Failed dependency:");
1213             changes.print();
1214             nm->print();
1215             nm->print_dependencies_on(tty);
1216             assert(false, "Should have been marked for deoptimization");
1217           }
1218         }
1219       }
1220     }
1221   }
1222 }
1223 #endif
1224 
1225 void CodeCache::mark_for_deoptimization(DeoptimizationScope* deopt_scope, KlassDepChange& changes) {
1226   MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1227 
1228   // search the hierarchy looking for nmethods which are affected by the loading of this class
1229 
1230   // then search the interfaces this class implements looking for nmethods
1231   // which might be dependent of the fact that an interface only had one
1232   // implementor.
1233   // nmethod::check_all_dependencies works only correctly, if no safepoint
1234   // can happen
1235   NoSafepointVerifier nsv;
1236   for (DepChange::ContextStream str(changes, nsv); str.next(); ) {
1237     InstanceKlass* d = str.klass();







1238     d->mark_dependent_nmethods(deopt_scope, changes);
1239   }
1240 
1241 #ifndef PRODUCT
1242   if (VerifyDependencies) {
1243     // Object pointers are used as unique identifiers for dependency arguments. This
1244     // is only possible if no safepoint, i.e., GC occurs during the verification code.
1245     dependentCheckTime.start();
1246     check_live_nmethods_dependencies(changes);
1247     dependentCheckTime.stop();
1248   }
1249 #endif
1250 }
1251 
1252 #if INCLUDE_JVMTI
1253 // RedefineClasses support for saving nmethods that are dependent on "old" methods.
1254 // We don't really expect this table to grow very large.  If it does, it can become a hashtable.
1255 static GrowableArray<nmethod*>* old_nmethod_table = nullptr;
1256 
1257 static void add_to_old_table(nmethod* c) {

1476 }
1477 PRAGMA_DIAG_POP
1478 
1479 void CodeCache::print_memory_overhead() {
1480   size_t wasted_bytes = 0;
1481   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1482       CodeHeap* curr_heap = *heap;
1483       for (CodeBlob* cb = (CodeBlob*)curr_heap->first(); cb != nullptr; cb = (CodeBlob*)curr_heap->next(cb)) {
1484         HeapBlock* heap_block = ((HeapBlock*)cb) - 1;
1485         wasted_bytes += heap_block->length() * CodeCacheSegmentSize - cb->size();
1486       }
1487   }
1488   // Print bytes that are allocated in the freelist
1489   ttyLocker ttl;
1490   tty->print_cr("Number of elements in freelist: %zd",       freelists_length());
1491   tty->print_cr("Allocated in freelist:          %zdkB",  bytes_allocated_in_freelists()/K);
1492   tty->print_cr("Unused bytes in CodeBlobs:      %zdkB",  (wasted_bytes/K));
1493   tty->print_cr("Segment map size:               %zdkB",  allocated_segments()/K); // 1 byte per segment
1494 }
1495 















































































1496 //------------------------------------------------------------------------------------------------
1497 // Non-product version
1498 
1499 #ifndef PRODUCT
1500 
1501 void CodeCache::print_trace(const char* event, CodeBlob* cb, uint size) {
1502   if (PrintCodeCache2) {  // Need to add a new flag
1503     ResourceMark rm;
1504     if (size == 0) {
1505       int s = cb->size();
1506       assert(s >= 0, "CodeBlob size is negative: %d", s);
1507       size = (uint) s;
1508     }
1509     tty->print_cr("CodeCache %s:  addr: " INTPTR_FORMAT ", size: 0x%x", event, p2i(cb), size);
1510   }
1511 }
1512 
1513 void CodeCache::print_internals() {
1514   int nmethodCount = 0;
1515   int runtimeStubCount = 0;
1516   int upcallStubCount = 0;
1517   int adapterCount = 0;
1518   int mhAdapterCount = 0;
1519   int vtableBlobCount = 0;
1520   int deoptimizationStubCount = 0;
1521   int uncommonTrapStubCount = 0;
1522   int exceptionStubCount = 0;
1523   int safepointStubCount = 0;
1524   int bufferBlobCount = 0;
1525   int total = 0;
1526   int nmethodNotEntrant = 0;
1527   int nmethodJava = 0;
1528   int nmethodNative = 0;
1529   int max_nm_size = 0;
1530   ResourceMark rm;
1531 
1532   int i = 0;
1533   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1534     if ((_nmethod_heaps->length() >= 1) && Verbose) {
1535       tty->print_cr("-- %s --", (*heap)->name());
1536     }
1537     FOR_ALL_BLOBS(cb, *heap) {
1538       total++;

1539       if (cb->is_nmethod()) {
1540         nmethod* nm = (nmethod*)cb;
1541 
1542         if (Verbose && nm->method() != nullptr) {
1543           ResourceMark rm;
1544           char *method_name = nm->method()->name_and_sig_as_C_string();
1545           tty->print("%s", method_name);
1546           if(nm->is_not_entrant()) { tty->print_cr(" not-entrant"); }
1547         }
1548 
1549         nmethodCount++;
1550 
1551         if(nm->is_not_entrant()) { nmethodNotEntrant++; }
1552         if(nm->method() != nullptr && nm->is_native_method()) { nmethodNative++; }
1553 
1554         if(nm->method() != nullptr && nm->is_java_method()) {
1555           nmethodJava++;
1556           max_nm_size = MAX2(max_nm_size, nm->size());
1557         }
1558       } else if (cb->is_runtime_stub()) {
1559         runtimeStubCount++;
1560       } else if (cb->is_upcall_stub()) {
1561         upcallStubCount++;
1562       } else if (cb->is_deoptimization_stub()) {
1563         deoptimizationStubCount++;
1564       } else if (cb->is_uncommon_trap_stub()) {
1565         uncommonTrapStubCount++;
1566       } else if (cb->is_exception_stub()) {
1567         exceptionStubCount++;

1724       FOR_ALL_BLOBS(cb, *heap) {
1725         number_of_blobs++;
1726         code_size += cb->code_size();
1727         ImmutableOopMapSet* set = cb->oop_maps();
1728         if (set != nullptr) {
1729           number_of_oop_maps += set->count();
1730           map_size           += set->nr_of_bytes();
1731         }
1732       }
1733     }
1734     tty->print_cr("OopMaps");
1735     tty->print_cr("  #blobs    = %d", number_of_blobs);
1736     tty->print_cr("  code size = %d", code_size);
1737     tty->print_cr("  #oop_maps = %d", number_of_oop_maps);
1738     tty->print_cr("  map size  = %d", map_size);
1739   }
1740 
1741 #endif // !PRODUCT
1742 }
1743 



















1744 void CodeCache::print_summary(outputStream* st, bool detailed) {
1745   int full_count = 0;
1746   julong total_used = 0;
1747   julong total_max_used = 0;
1748   julong total_free = 0;
1749   julong total_size = 0;
1750   FOR_ALL_HEAPS(heap_iterator) {
1751     CodeHeap* heap = (*heap_iterator);
1752     size_t total = (heap->high_boundary() - heap->low_boundary());
1753     if (_heaps->length() >= 1) {
1754       st->print("%s:", heap->name());
1755     } else {
1756       st->print("CodeCache:");
1757     }
1758     size_t size = total/K;
1759     size_t used = (total - heap->unallocated_capacity())/K;
1760     size_t max_used = heap->max_allocated_capacity()/K;
1761     size_t free = heap->unallocated_capacity()/K;
1762     total_size += size;
1763     total_used += used;

   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 "cds/cdsAccess.hpp"
  26 #include "code/codeBlob.hpp"
  27 #include "code/codeCache.hpp"
  28 #include "code/codeHeapState.hpp"
  29 #include "code/compiledIC.hpp"
  30 #include "code/dependencies.hpp"
  31 #include "code/dependencyContext.hpp"
  32 #include "code/nmethod.hpp"
  33 #include "code/pcDesc.hpp"
  34 #include "compiler/compilationPolicy.hpp"
  35 #include "compiler/compileBroker.hpp"
  36 #include "compiler/compilerDefinitions.inline.hpp"
  37 #include "compiler/oopMap.hpp"
  38 #include "gc/shared/barrierSetNMethod.hpp"
  39 #include "gc/shared/classUnloadingContext.hpp"
  40 #include "gc/shared/collectedHeap.hpp"
  41 #include "jfr/jfrEvents.hpp"
  42 #include "jvm_io.h"
  43 #include "logging/log.hpp"
  44 #include "logging/logStream.hpp"
  45 #include "memory/allocation.inline.hpp"

 154       scopes_data_size += nm->scopes_data_size();
 155       scopes_pcs_size  += nm->scopes_pcs_size();
 156     } else {
 157       code_size        += cb->code_size();
 158     }
 159   }
 160 };
 161 
 162 // Iterate over all CodeHeaps
 163 #define FOR_ALL_HEAPS(heap) for (GrowableArrayIterator<CodeHeap*> heap = _heaps->begin(); heap != _heaps->end(); ++heap)
 164 #define FOR_ALL_ALLOCABLE_HEAPS(heap) for (GrowableArrayIterator<CodeHeap*> heap = _allocable_heaps->begin(); heap != _allocable_heaps->end(); ++heap)
 165 
 166 // Iterate over all CodeBlobs (cb) on the given CodeHeap
 167 #define FOR_ALL_BLOBS(cb, heap) for (CodeBlob* cb = first_blob(heap); cb != nullptr; cb = next_blob(heap, cb))
 168 
 169 address CodeCache::_low_bound = nullptr;
 170 address CodeCache::_high_bound = nullptr;
 171 volatile int CodeCache::_number_of_nmethods_with_dependencies = 0;
 172 ExceptionCache* volatile CodeCache::_exception_cache_purge_list = nullptr;
 173 
 174 static ReservedSpace _cds_code_space;
 175 
 176 // Initialize arrays of CodeHeap subsets
 177 GrowableArray<CodeHeap*>* CodeCache::_heaps = new(mtCode) GrowableArray<CodeHeap*> (static_cast<int>(CodeBlobType::All), mtCode);
 178 GrowableArray<CodeHeap*>* CodeCache::_nmethod_heaps = new(mtCode) GrowableArray<CodeHeap*> (static_cast<int>(CodeBlobType::All), mtCode);
 179 GrowableArray<CodeHeap*>* CodeCache::_allocable_heaps = new(mtCode) GrowableArray<CodeHeap*> (static_cast<int>(CodeBlobType::All), mtCode);
 180 
 181 static void check_min_size(const char* codeheap, size_t size, size_t required_size) {
 182   if (size < required_size) {
 183     log_debug(codecache)("Code heap (%s) size %zuK below required minimal size %zuK",
 184                          codeheap, size/K, required_size/K);
 185     err_msg title("Not enough space in %s to run VM", codeheap);
 186     err_msg message("%zuK < %zuK", size/K, required_size/K);
 187     vm_exit_during_initialization(title, message);
 188   }
 189 }
 190 
 191 struct CodeHeapInfo {
 192   size_t size;
 193   bool set;
 194   bool enabled;
 195 };
 196 
 197 static void set_size_of_unset_code_heap(CodeHeapInfo* heap, size_t available_size, size_t used_size, size_t min_size) {
 198   assert(!heap->set, "sanity");
 199   heap->size = (available_size > (used_size + min_size)) ? (available_size - used_size) : min_size;
 200 }
 201 
 202 void CodeCache::initialize_heaps() {

 203   CodeHeapInfo non_nmethod = {NonNMethodCodeHeapSize, FLAG_IS_CMDLINE(NonNMethodCodeHeapSize), true};
 204   CodeHeapInfo profiled = {ProfiledCodeHeapSize, FLAG_IS_CMDLINE(ProfiledCodeHeapSize), true};
 205   CodeHeapInfo non_profiled = {NonProfiledCodeHeapSize, FLAG_IS_CMDLINE(NonProfiledCodeHeapSize), true};
 206 
 207   const bool cache_size_set   = FLAG_IS_CMDLINE(ReservedCodeCacheSize);
 208   const size_t ps             = page_size(false, 8);
 209   const size_t min_size       = MAX2(os::vm_allocation_granularity(), ps);
 210   const size_t min_cache_size = CodeCacheMinimumUseSpace DEBUG_ONLY(* 3); // Make sure we have enough space for VM internal code
 211   size_t cache_size           = align_up(ReservedCodeCacheSize, min_size);
 212 
 213   // Prerequisites
 214   if (!heap_available(CodeBlobType::MethodProfiled)) {
 215     // For compatibility reasons, disabled tiered compilation overrides
 216     // segment size even if it is set explicitly.
 217     non_profiled.size += profiled.size;
 218     // Profiled code heap is not available, forcibly set size to 0
 219     profiled.size = 0;
 220     profiled.set = true;
 221     profiled.enabled = false;
 222   }
 223 
 224   assert(heap_available(CodeBlobType::MethodNonProfiled), "MethodNonProfiled heap is always available for segmented code heap");
 225 
 226   size_t compiler_buffer_size = 0;
 227   COMPILER1_PRESENT(compiler_buffer_size += CompilationPolicy::c1_count() * Compiler::code_buffer_size());
 228   COMPILER2_PRESENT(compiler_buffer_size += CompilationPolicy::c2_count() * C2Compiler::initial_code_buffer_size());
 229   COMPILER2_PRESENT(compiler_buffer_size += (CompilationPolicy::c2_count() + CompilationPolicy::c3_count()) * C2Compiler::initial_code_buffer_size());
 230 
 231   if (!non_nmethod.set) {
 232     non_nmethod.size += compiler_buffer_size;
 233     // Further down, just before FLAG_SET_ERGO(), all segment sizes are
 234     // aligned down to the next lower multiple of min_size. For large page
 235     // sizes, this may result in (non_nmethod.size == 0) which is not acceptable.
 236     // Therefore, force non_nmethod.size to at least min_size.
 237     non_nmethod.size = MAX2(non_nmethod.size, min_size);
 238   }
 239 
 240   if (!profiled.set && !non_profiled.set) {
 241     non_profiled.size = profiled.size = (cache_size > non_nmethod.size + 2 * min_size) ?
 242                                         (cache_size - non_nmethod.size) / 2 : min_size;
 243   }
 244 
 245   if (profiled.set && !non_profiled.set) {
 246     set_size_of_unset_code_heap(&non_profiled, cache_size, non_nmethod.size + profiled.size, min_size);
 247   }
 248 
 249   if (!profiled.set && non_profiled.set) {

 304     if (ps < lg_ps) {
 305       log_warning(codecache)("Code cache size too small for " PROPERFMT " pages. "
 306                              "Reverting to smaller page size (" PROPERFMT ").",
 307                              PROPERFMTARGS(lg_ps), PROPERFMTARGS(ps));
 308     }
 309   }
 310 
 311   // Note: if large page support is enabled, min_size is at least the large
 312   // page size. This ensures that the code cache is covered by large pages.
 313   non_profiled.size += non_nmethod.size & alignment_mask(min_size);
 314   non_profiled.size += profiled.size & alignment_mask(min_size);
 315   non_nmethod.size = align_down(non_nmethod.size, min_size);
 316   profiled.size = align_down(profiled.size, min_size);
 317   non_profiled.size = align_down(non_profiled.size, min_size);
 318 
 319   FLAG_SET_ERGO(NonNMethodCodeHeapSize, non_nmethod.size);
 320   FLAG_SET_ERGO(ProfiledCodeHeapSize, profiled.size);
 321   FLAG_SET_ERGO(NonProfiledCodeHeapSize, non_profiled.size);
 322   FLAG_SET_ERGO(ReservedCodeCacheSize, cache_size);
 323 
 324   const size_t cds_code_size = align_up(CDSAccess::get_cached_code_size(), min_size);
 325   cache_size += cds_code_size;
 326 
 327   ReservedSpace rs = reserve_heap_memory(cache_size, ps);
 328 
 329   // Register CodeHeaps with LSan as we sometimes embed pointers to malloc memory.
 330   LSAN_REGISTER_ROOT_REGION(rs.base(), rs.size());
 331 
 332   size_t offset = 0;
 333   if (cds_code_size > 0) {
 334     // FIXME: use CodeHeapInfo for this hack ...
 335     _cds_code_space = rs.partition(offset, cds_code_size);
 336     offset += cds_code_size;
 337   }
 338 
 339   if (profiled.enabled) {
 340     ReservedSpace profiled_space = rs.partition(offset, profiled.size);
 341     offset += profiled.size;
 342     // Tier 2 and tier 3 (profiled) methods
 343     add_heap(profiled_space, "CodeHeap 'profiled nmethods'", CodeBlobType::MethodProfiled);
 344   }
 345 
 346   ReservedSpace non_method_space = rs.partition(offset, non_nmethod.size);
 347   offset += non_nmethod.size;
 348   // Non-nmethods (stubs, adapters, ...)
 349   add_heap(non_method_space, "CodeHeap 'non-nmethods'", CodeBlobType::NonNMethod);
 350 
 351   if (non_profiled.enabled) {
 352     ReservedSpace non_profiled_space  = rs.partition(offset, non_profiled.size);
 353     // Tier 1 and tier 4 (non-profiled) methods and native methods
 354     add_heap(non_profiled_space, "CodeHeap 'non-profiled nmethods'", CodeBlobType::MethodNonProfiled);
 355   }
 356 }
 357 
 358 void* CodeCache::map_cached_code() {
 359   if (_cds_code_space.size() > 0 && CDSAccess::map_cached_code(_cds_code_space)) {
 360     return _cds_code_space.base();
 361   } else {
 362     return nullptr;
 363   }
 364 }
 365 
 366 size_t CodeCache::page_size(bool aligned, size_t min_pages) {
 367   return aligned ? os::page_size_for_region_aligned(ReservedCodeCacheSize, min_pages) :
 368                    os::page_size_for_region_unaligned(ReservedCodeCacheSize, min_pages);
 369 }
 370 
 371 ReservedSpace CodeCache::reserve_heap_memory(size_t size, size_t rs_ps) {
 372   // Align and reserve space for code cache
 373   const size_t rs_align = MAX2(rs_ps, os::vm_allocation_granularity());
 374   const size_t rs_size = align_up(size, rs_align);
 375 
 376   ReservedSpace rs = CodeMemoryReserver::reserve(rs_size, rs_align, rs_ps);
 377   if (!rs.is_reserved()) {
 378     vm_exit_during_initialization(err_msg("Could not reserve enough space for code cache (%zuK)",
 379                                           rs_size/K));
 380   }
 381 
 382   // Initialize bounds
 383   _low_bound = (address)rs.base();
 384   _high_bound = _low_bound + rs.size();
 385   return rs;

1208                             AnyObj::RESOURCE_AREA, mtInternal,
1209                             &DependencySignature::hash,
1210                             &DependencySignature::equals> DepTable;
1211 
1212   DepTable* table = new DepTable();
1213 
1214   // Iterate over live nmethods and check dependencies of all nmethods that are not
1215   // marked for deoptimization. A particular dependency is only checked once.
1216   NMethodIterator iter(NMethodIterator::not_unloading);
1217   while(iter.next()) {
1218     nmethod* nm = iter.method();
1219     // Only notify for live nmethods
1220     if (!nm->is_marked_for_deoptimization()) {
1221       for (Dependencies::DepStream deps(nm); deps.next(); ) {
1222         // Construct abstraction of a dependency.
1223         DependencySignature* current_sig = new DependencySignature(deps);
1224 
1225         // Determine if dependency is already checked. table->put(...) returns
1226         // 'true' if the dependency is added (i.e., was not in the hashtable).
1227         if (table->put(*current_sig, 1)) {
1228           Klass* witness = deps.check_dependency();
1229           if (witness != nullptr) {
1230             // Dependency checking failed. Print out information about the failed
1231             // dependency and finally fail with an assert. We can fail here, since
1232             // dependency checking is never done in a product build.
1233             deps.print_dependency(tty, witness, true);
1234             changes.print();
1235             nm->print();
1236             nm->print_dependencies_on(tty);
1237             assert(false, "Should have been marked for deoptimization");
1238           }
1239         }
1240       }
1241     }
1242   }
1243 }
1244 #endif
1245 
1246 void CodeCache::mark_for_deoptimization(DeoptimizationScope* deopt_scope, KlassDepChange& changes) {
1247   MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1248 
1249   // search the hierarchy looking for nmethods which are affected by the loading of this class
1250 
1251   // then search the interfaces this class implements looking for nmethods
1252   // which might be dependent of the fact that an interface only had one
1253   // implementor.
1254   // nmethod::check_all_dependencies works only correctly, if no safepoint
1255   // can happen
1256   NoSafepointVerifier nsv;
1257   for (DepChange::ContextStream str(changes, nsv); str.next(); ) {
1258     InstanceKlass* d = str.klass();
1259     {
1260       LogStreamHandle(Trace, dependencies) log;
1261       if (log.is_enabled()) {
1262         log.print("Processing context ");
1263         d->name()->print_value_on(&log);
1264       }
1265     }
1266     d->mark_dependent_nmethods(deopt_scope, changes);
1267   }
1268 
1269 #ifndef PRODUCT
1270   if (VerifyDependencies) {
1271     // Object pointers are used as unique identifiers for dependency arguments. This
1272     // is only possible if no safepoint, i.e., GC occurs during the verification code.
1273     dependentCheckTime.start();
1274     check_live_nmethods_dependencies(changes);
1275     dependentCheckTime.stop();
1276   }
1277 #endif
1278 }
1279 
1280 #if INCLUDE_JVMTI
1281 // RedefineClasses support for saving nmethods that are dependent on "old" methods.
1282 // We don't really expect this table to grow very large.  If it does, it can become a hashtable.
1283 static GrowableArray<nmethod*>* old_nmethod_table = nullptr;
1284 
1285 static void add_to_old_table(nmethod* c) {

1504 }
1505 PRAGMA_DIAG_POP
1506 
1507 void CodeCache::print_memory_overhead() {
1508   size_t wasted_bytes = 0;
1509   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1510       CodeHeap* curr_heap = *heap;
1511       for (CodeBlob* cb = (CodeBlob*)curr_heap->first(); cb != nullptr; cb = (CodeBlob*)curr_heap->next(cb)) {
1512         HeapBlock* heap_block = ((HeapBlock*)cb) - 1;
1513         wasted_bytes += heap_block->length() * CodeCacheSegmentSize - cb->size();
1514       }
1515   }
1516   // Print bytes that are allocated in the freelist
1517   ttyLocker ttl;
1518   tty->print_cr("Number of elements in freelist: %zd",       freelists_length());
1519   tty->print_cr("Allocated in freelist:          %zdkB",  bytes_allocated_in_freelists()/K);
1520   tty->print_cr("Unused bytes in CodeBlobs:      %zdkB",  (wasted_bytes/K));
1521   tty->print_cr("Segment map size:               %zdkB",  allocated_segments()/K); // 1 byte per segment
1522 }
1523 
1524 static void print_helper1(outputStream* st, const char* prefix, int total, int not_entrant, int used) {
1525   if (total > 0) {
1526     double ratio = (100.0 * used) / total;
1527     st->print("%s %3d nmethods: %3d not_entrant, %d used (%2.1f%%)", prefix, total, not_entrant, used, ratio);
1528   }
1529 }
1530 
1531 void CodeCache::print_nmethod_statistics_on(outputStream* st) {
1532   int stats     [2][6][3][2] = {0};
1533   int stats_used[2][6][3][2] = {0};
1534 
1535   int total_osr = 0;
1536   int total_entrant = 0;
1537   int total_non_entrant = 0;
1538   int total_other = 0;
1539   int total_used = 0;
1540 
1541   NMethodIterator iter(NMethodIterator::all);
1542   while (iter.next()) {
1543     nmethod* nm = iter.method();
1544     if (nm->is_in_use()) {
1545       ++total_entrant;
1546     } else if (nm->is_not_entrant()) {
1547       ++total_non_entrant;
1548     } else {
1549       ++total_other;
1550     }
1551     if (nm->is_osr_method()) {
1552       ++total_osr;
1553     }
1554     if (nm->used()) {
1555       ++total_used;
1556     }
1557     assert(!nm->preloaded() || nm->comp_level() == CompLevel_full_optimization, "");
1558 
1559     int idx1 = nm->is_aot() ? 1 : 0;
1560     int idx2 = nm->comp_level() + (nm->preloaded() ? 1 : 0);
1561     int idx3 = (nm->is_in_use()      ? 0 :
1562                (nm->is_not_entrant() ? 1 :
1563                                        2));
1564     int idx4 = (nm->is_osr_method() ? 1 : 0);
1565     stats[idx1][idx2][idx3][idx4] += 1;
1566     if (nm->used()) {
1567       stats_used[idx1][idx2][idx3][idx4] += 1;
1568     }
1569   }
1570 
1571   st->print("Total: %d methods (%d entrant / %d not_entrant; osr: %d ",
1572                total_entrant + total_non_entrant + total_other,
1573                total_entrant, total_non_entrant, total_osr);
1574   if (total_other > 0) {
1575     st->print("; %d other", total_other);
1576   }
1577   st->print_cr(")");
1578 
1579   for (int i = CompLevel_simple; i <= CompLevel_full_optimization; i++) {
1580     int total_normal = stats[0][i][0][0] + stats[0][i][1][0] + stats[0][i][2][0];
1581     int total_osr    = stats[0][i][0][1] + stats[0][i][1][1] + stats[0][i][2][1];
1582     if (total_normal + total_osr > 0) {
1583       st->print("  Tier%d:", i);
1584       print_helper1(st,      "", total_normal, stats[0][i][1][0], stats_used[0][i][0][0] + stats_used[0][i][1][0]);
1585       print_helper1(st, "; osr:", total_osr,    stats[0][i][1][1], stats_used[0][i][0][1] + stats_used[0][i][1][1]);
1586       st->cr();
1587     }
1588   }
1589   st->cr();
1590   for (int i = CompLevel_simple; i <= CompLevel_full_optimization + 1; i++) {
1591     int total_normal = stats[1][i][0][0] + stats[1][i][1][0] + stats[1][i][2][0];
1592     int total_osr    = stats[1][i][0][1] + stats[1][i][1][1] + stats[1][i][2][1];
1593     assert(total_osr == 0, "sanity");
1594     if (total_normal + total_osr > 0) {
1595       st->print("  AOT Code T%d:", i);
1596       print_helper1(st,      "", total_normal, stats[1][i][1][0], stats_used[1][i][0][0] + stats_used[1][i][1][0]);
1597       print_helper1(st, "; osr:", total_osr,    stats[1][i][1][1], stats_used[1][i][0][1] + stats_used[1][i][1][1]);
1598       st->cr();
1599     }
1600   }
1601 }
1602 
1603 //------------------------------------------------------------------------------------------------
1604 // Non-product version
1605 
1606 #ifndef PRODUCT
1607 
1608 void CodeCache::print_trace(const char* event, CodeBlob* cb, uint size) {
1609   if (PrintCodeCache2) {  // Need to add a new flag
1610     ResourceMark rm;
1611     if (size == 0) {
1612       int s = cb->size();
1613       assert(s >= 0, "CodeBlob size is negative: %d", s);
1614       size = (uint) s;
1615     }
1616     tty->print_cr("CodeCache %s:  addr: " INTPTR_FORMAT ", size: 0x%x", event, p2i(cb), size);
1617   }
1618 }
1619 
1620 void CodeCache::print_internals() {
1621   int nmethodCount = 0;
1622   int runtimeStubCount = 0;
1623   int upcallStubCount = 0;
1624   int adapterCount = 0;
1625   int mhAdapterCount = 0;
1626   int vtableBlobCount = 0;
1627   int deoptimizationStubCount = 0;
1628   int uncommonTrapStubCount = 0;
1629   int exceptionStubCount = 0;
1630   int safepointStubCount = 0;
1631   int bufferBlobCount = 0;
1632   int total = 0;
1633   int nmethodNotEntrant = 0;
1634   int nmethodJava = 0;
1635   int nmethodNative = 0;
1636   int max_nm_size = 0;
1637   ResourceMark rm;
1638 
1639   int i = 0;
1640   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1641     int heap_total = 0;
1642     tty->print_cr("-- %s --", (*heap)->name());

1643     FOR_ALL_BLOBS(cb, *heap) {
1644       total++;
1645       heap_total++;
1646       if (cb->is_nmethod()) {
1647         nmethod* nm = (nmethod*)cb;
1648 
1649         tty->print("%4d: ", heap_total);
1650         CompileTask::print(tty, nm, (nm->is_not_entrant() ? "non-entrant" : ""), true, true);




1651 
1652         nmethodCount++;
1653 
1654         if(nm->is_not_entrant()) { nmethodNotEntrant++; }
1655         if(nm->method() != nullptr && nm->is_native_method()) { nmethodNative++; }
1656 
1657         if(nm->method() != nullptr && nm->is_java_method()) {
1658           nmethodJava++;
1659           max_nm_size = MAX2(max_nm_size, nm->size());
1660         }
1661       } else if (cb->is_runtime_stub()) {
1662         runtimeStubCount++;
1663       } else if (cb->is_upcall_stub()) {
1664         upcallStubCount++;
1665       } else if (cb->is_deoptimization_stub()) {
1666         deoptimizationStubCount++;
1667       } else if (cb->is_uncommon_trap_stub()) {
1668         uncommonTrapStubCount++;
1669       } else if (cb->is_exception_stub()) {
1670         exceptionStubCount++;

1827       FOR_ALL_BLOBS(cb, *heap) {
1828         number_of_blobs++;
1829         code_size += cb->code_size();
1830         ImmutableOopMapSet* set = cb->oop_maps();
1831         if (set != nullptr) {
1832           number_of_oop_maps += set->count();
1833           map_size           += set->nr_of_bytes();
1834         }
1835       }
1836     }
1837     tty->print_cr("OopMaps");
1838     tty->print_cr("  #blobs    = %d", number_of_blobs);
1839     tty->print_cr("  code size = %d", code_size);
1840     tty->print_cr("  #oop_maps = %d", number_of_oop_maps);
1841     tty->print_cr("  map size  = %d", map_size);
1842   }
1843 
1844 #endif // !PRODUCT
1845 }
1846 
1847 void CodeCache::print_nmethods_on(outputStream* st) {
1848   ResourceMark rm;
1849   int i = 0;
1850   FOR_ALL_ALLOCABLE_HEAPS(heap) {
1851     st->print_cr("-- %s --", (*heap)->name());
1852     FOR_ALL_BLOBS(cb, *heap) {
1853       i++;
1854       if (cb->is_nmethod()) {
1855         nmethod* nm = (nmethod*)cb;
1856         st->print("%4d: ", i);
1857         CompileTask::print(st, nm, nullptr, true, false);
1858 
1859         const char non_entrant_char = (nm->is_not_entrant() ? 'N' : ' ');
1860         st->print_cr(" %c", non_entrant_char);
1861       }
1862     }
1863   }
1864 }
1865 
1866 void CodeCache::print_summary(outputStream* st, bool detailed) {
1867   int full_count = 0;
1868   julong total_used = 0;
1869   julong total_max_used = 0;
1870   julong total_free = 0;
1871   julong total_size = 0;
1872   FOR_ALL_HEAPS(heap_iterator) {
1873     CodeHeap* heap = (*heap_iterator);
1874     size_t total = (heap->high_boundary() - heap->low_boundary());
1875     if (_heaps->length() >= 1) {
1876       st->print("%s:", heap->name());
1877     } else {
1878       st->print("CodeCache:");
1879     }
1880     size_t size = total/K;
1881     size_t used = (total - heap->unallocated_capacity())/K;
1882     size_t max_used = heap->max_allocated_capacity()/K;
1883     size_t free = heap->unallocated_capacity()/K;
1884     total_size += size;
1885     total_used += used;
< prev index next >