1 /*
   2  * Copyright (c) 1997, 2019, 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 #ifndef SHARE_CODE_CODECACHE_HPP
  26 #define SHARE_CODE_CODECACHE_HPP
  27 
  28 #include "code/codeBlob.hpp"
  29 #include "code/nmethod.hpp"
  30 #include "gc/shared/gcBehaviours.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "memory/heap.hpp"
  33 #include "oops/instanceKlass.hpp"
  34 #include "oops/oopsHierarchy.hpp"
  35 #include "runtime/mutexLocker.hpp"
  36 
  37 // The CodeCache implements the code cache for various pieces of generated
  38 // code, e.g., compiled java methods, runtime stubs, transition frames, etc.
  39 // The entries in the CodeCache are all CodeBlob's.
  40 
  41 // -- Implementation --
  42 // The CodeCache consists of one or more CodeHeaps, each of which contains
  43 // CodeBlobs of a specific CodeBlobType. Currently heaps for the following
  44 // types are available:
  45 //  - Non-nmethods: Non-nmethods like Buffers, Adapters and Runtime Stubs
  46 //  - Profiled nmethods: nmethods that are profiled, i.e., those
  47 //    executed at level 2 or 3
  48 //  - Non-Profiled nmethods: nmethods that are not profiled, i.e., those
  49 //    executed at level 1 or 4 and native methods
  50 //  - All: Used for code of all types if code cache segmentation is disabled.
  51 //
  52 // In the rare case of the non-nmethod code heap getting full, non-nmethod code
  53 // will be stored in the non-profiled code heap as a fallback solution.
  54 //
  55 // Depending on the availability of compilers and TieredCompilation there
  56 // may be fewer heaps. The size of the code heaps depends on the values of
  57 // ReservedCodeCacheSize, NonProfiledCodeHeapSize and ProfiledCodeHeapSize
  58 // (see CodeCache::heap_available(..) and CodeCache::initialize_heaps(..)
  59 // for details).
  60 //
  61 // Code cache segmentation is controlled by the flag SegmentedCodeCache.
  62 // If turned off, all code types are stored in a single code heap. By default
  63 // code cache segmentation is turned on if TieredCompilation is enabled and
  64 // ReservedCodeCacheSize >= 240 MB.
  65 //
  66 // All methods of the CodeCache accepting a CodeBlobType only apply to
  67 // CodeBlobs of the given type. For example, iteration over the
  68 // CodeBlobs of a specific type can be done by using CodeCache::first_blob(..)
  69 // and CodeCache::next_blob(..) and providing the corresponding CodeBlobType.
  70 //
  71 // IMPORTANT: If you add new CodeHeaps to the code cache or change the
  72 // existing ones, make sure to adapt the dtrace scripts (jhelper.d) for
  73 // Solaris and BSD.
  74 
  75 class ExceptionCache;
  76 class KlassDepChange;
  77 class OopClosure;
  78 class ShenandoahParallelCodeHeapIterator;
  79 class NativePostCallNop;
  80 
  81 class CodeCache : AllStatic {
  82   friend class VMStructs;
  83   friend class JVMCIVMStructs;
  84   template <class T, class Filter> friend class CodeBlobIterator;
  85   friend class WhiteBox;
  86   friend class CodeCacheLoader;
  87   friend class ShenandoahParallelCodeHeapIterator;
  88  private:
  89   // CodeHeaps of the cache
  90   static GrowableArray<CodeHeap*>* _heaps;
  91   static GrowableArray<CodeHeap*>* _compiled_heaps;
  92   static GrowableArray<CodeHeap*>* _nmethod_heaps;
  93   static GrowableArray<CodeHeap*>* _allocable_heaps;
  94 
  95   static address _low_bound;                            // Lower bound of CodeHeap addresses
  96   static address _high_bound;                           // Upper bound of CodeHeap addresses
  97   static int _number_of_nmethods_with_dependencies;     // Total number of nmethods with dependencies
  98   static uint8_t _unloading_cycle;                      // Global state for recognizing old nmethods that need to be unloaded
  99 
 100   static ExceptionCache* volatile _exception_cache_purge_list;
 101 
 102   // CodeHeap management
 103   static void initialize_heaps();                             // Initializes the CodeHeaps
 104   // Check the code heap sizes set by the user via command line
 105   static void check_heap_sizes(size_t non_nmethod_size, size_t profiled_size, size_t non_profiled_size, size_t cache_size, bool all_set);
 106   // Creates a new heap with the given name and size, containing CodeBlobs of the given type
 107   static void add_heap(ReservedSpace rs, const char* name, int code_blob_type);
 108   static CodeHeap* get_code_heap_containing(void* p);         // Returns the CodeHeap containing the given pointer, or NULL
 109   static CodeHeap* get_code_heap(const CodeBlob* cb);         // Returns the CodeHeap for the given CodeBlob
 110   static CodeHeap* get_code_heap(int code_blob_type);         // Returns the CodeHeap for the given CodeBlobType
 111   // Returns the name of the VM option to set the size of the corresponding CodeHeap
 112   static const char* get_code_heap_flag_name(int code_blob_type);
 113   static ReservedCodeSpace reserve_heap_memory(size_t size);  // Reserves one continuous chunk of memory for the CodeHeaps
 114 
 115   // Iteration
 116   static CodeBlob* first_blob(CodeHeap* heap);                // Returns the first CodeBlob on the given CodeHeap
 117   static CodeBlob* first_blob(int code_blob_type);            // Returns the first CodeBlob of the given type
 118   static CodeBlob* next_blob(CodeHeap* heap, CodeBlob* cb);   // Returns the next CodeBlob on the given CodeHeap
 119 
 120   static size_t bytes_allocated_in_freelists();
 121   static int    allocated_segments();
 122   static size_t freelists_length();
 123 
 124   // Make private to prevent unsafe calls.  Not all CodeBlob*'s are embedded in a CodeHeap.
 125   static bool contains(CodeBlob *p) { fatal("don't call me!"); return false; }
 126   static CodeBlob* patch_nop(NativePostCallNop* nop, void* pc, int& slot);
 127 
 128  public:
 129   // Initialization
 130   static void initialize();
 131   static size_t page_size(bool aligned = true, size_t min_pages = 1); // Returns the page size used by the CodeCache
 132 
 133   static int code_heap_compare(CodeHeap* const &lhs, CodeHeap* const &rhs);
 134 
 135   static void add_heap(CodeHeap* heap);
 136   static const GrowableArray<CodeHeap*>* heaps() { return _heaps; }
 137   static const GrowableArray<CodeHeap*>* compiled_heaps() { return _compiled_heaps; }
 138   static const GrowableArray<CodeHeap*>* nmethod_heaps() { return _nmethod_heaps; }
 139 
 140   // Allocation/administration
 141   static CodeBlob* allocate(int size, int code_blob_type, int orig_code_blob_type = CodeBlobType::All); // allocates a new CodeBlob
 142   static void commit(CodeBlob* cb);                        // called when the allocated CodeBlob has been filled
 143   static int  alignment_unit();                            // guaranteed alignment of all CodeBlobs
 144   static int  alignment_offset();                          // guaranteed offset of first CodeBlob byte within alignment unit (i.e., allocation header)
 145   static void free(CodeBlob* cb);                          // frees a CodeBlob
 146   static void free_unused_tail(CodeBlob* cb, size_t used); // frees the unused tail of a CodeBlob (only used by TemplateInterpreter::initialize())
 147   static bool contains(void *p);                           // returns whether p is included
 148   static bool contains(nmethod* nm);                       // returns whether nm is included
 149   static void blobs_do(void f(CodeBlob* cb));              // iterates over all CodeBlobs
 150   static void blobs_do(CodeBlobClosure* f);                // iterates over all CodeBlobs
 151   static void nmethods_do(void f(nmethod* nm));            // iterates over all nmethods
 152   static void metadata_do(MetadataClosure* f);             // iterates over metadata in alive nmethods
 153 
 154   // Lookup
 155   static CodeBlob* find_blob(void* start);              // Returns the CodeBlob containing the given address
 156   static CodeBlob* find_blob_unsafe(void* start);       // Same as find_blob but does not fail if looking up a zombie method
 157   static CodeBlob* find_blob_fast(void* start);         // Returns the CodeBlob containing the given address
 158   static CodeBlob* find_blob_and_oopmap(void* start, int& slot);         // Returns the CodeBlob containing the given address
 159   static nmethod*  find_nmethod(void* start);           // Returns the nmethod containing the given address
 160   static CompiledMethod* find_compiled(void* start);
 161 
 162   static int       blob_count();                        // Returns the total number of CodeBlobs in the cache
 163   static int       blob_count(int code_blob_type);
 164   static int       adapter_count();                     // Returns the total number of Adapters in the cache
 165   static int       adapter_count(int code_blob_type);
 166   static int       nmethod_count();                     // Returns the total number of nmethods in the cache
 167   static int       nmethod_count(int code_blob_type);
 168 
 169   // GC support
 170   static void verify_oops();
 171   // If any oops are not marked this method unloads (i.e., breaks root links
 172   // to) any unmarked codeBlobs in the cache.  Sets "marked_for_unloading"
 173   // to "true" iff some code got unloaded.
 174   // "unloading_occurred" controls whether metadata should be cleaned because of class unloading.
 175   class UnloadingScope: StackObj {
 176     ClosureIsUnloadingBehaviour _is_unloading_behaviour;
 177     IsUnloadingBehaviour*       _saved_behaviour;
 178 
 179   public:
 180     UnloadingScope(BoolObjectClosure* is_alive);
 181     ~UnloadingScope();
 182   };
 183 
 184   static void do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred);
 185   static uint8_t unloading_cycle() { return _unloading_cycle; }
 186   static void increment_unloading_cycle();
 187   static void release_exception_cache(ExceptionCache* entry);
 188   static void purge_exception_caches();
 189 
 190   // Printing/debugging
 191   static void print();                           // prints summary
 192   static void print_internals();
 193   static void print_memory_overhead();
 194   static void verify();                          // verifies the code cache
 195   static void print_trace(const char* event, CodeBlob* cb, int size = 0) PRODUCT_RETURN;
 196   static void print_summary(outputStream* st, bool detailed = true); // Prints a summary of the code cache usage
 197   static void log_state(outputStream* st);
 198   static const char* get_code_heap_name(int code_blob_type)  { return (heap_available(code_blob_type) ? get_code_heap(code_blob_type)->name() : "Unused"); }
 199   static void report_codemem_full(int code_blob_type, bool print);
 200 
 201   // Dcmd (Diagnostic commands)
 202   static void print_codelist(outputStream* st);
 203   static void print_layout(outputStream* st);
 204 
 205   // The full limits of the codeCache
 206   static address low_bound()                          { return _low_bound; }
 207   static address low_bound(int code_blob_type);
 208   static address high_bound()                         { return _high_bound; }
 209   static address high_bound(int code_blob_type);
 210 
 211   // Have to use far call instructions to call this pc.
 212   static bool is_far_target(address pc);
 213 
 214   // Profiling
 215   static size_t capacity();
 216   static size_t unallocated_capacity(int code_blob_type);
 217   static size_t unallocated_capacity();
 218   static size_t max_capacity();
 219 
 220   static double reverse_free_ratio(int code_blob_type);
 221 
 222   static void clear_inline_caches();                  // clear all inline caches
 223   static void cleanup_inline_caches();                // clean unloaded/zombie nmethods from inline caches
 224 
 225   // Returns true if an own CodeHeap for the given CodeBlobType is available
 226   static bool heap_available(int code_blob_type);
 227 
 228   // Returns the CodeBlobType for the given CompiledMethod
 229   static int get_code_blob_type(CompiledMethod* cm) {
 230     return get_code_heap(cm)->code_blob_type();
 231   }
 232 
 233   static bool code_blob_type_accepts_compiled(int type) {
 234     bool result = type == CodeBlobType::All || type <= CodeBlobType::MethodProfiled;
 235     AOT_ONLY( result = result || type == CodeBlobType::AOT; )
 236     return result;
 237   }
 238 
 239   static bool code_blob_type_accepts_nmethod(int type) {
 240     return type == CodeBlobType::All || type <= CodeBlobType::MethodProfiled;
 241   }
 242 
 243   static bool code_blob_type_accepts_allocable(int type) {
 244     return type <= CodeBlobType::All;
 245   }
 246 
 247 
 248   // Returns the CodeBlobType for the given compilation level
 249   static int get_code_blob_type(int comp_level) {
 250     if (comp_level == CompLevel_none ||
 251         comp_level == CompLevel_simple ||
 252         comp_level == CompLevel_full_optimization) {
 253       // Non profiled methods
 254       return CodeBlobType::MethodNonProfiled;
 255     } else if (comp_level == CompLevel_limited_profile ||
 256                comp_level == CompLevel_full_profile) {
 257       // Profiled methods
 258       return CodeBlobType::MethodProfiled;
 259     }
 260     ShouldNotReachHere();
 261     return 0;
 262   }
 263 
 264   static void verify_clean_inline_caches();
 265   static void verify_icholder_relocations();
 266 
 267   // Deoptimization
 268  private:
 269   static int  mark_for_deoptimization(KlassDepChange& changes);
 270 
 271  public:
 272   static void mark_all_nmethods_for_deoptimization();
 273   static int  mark_for_deoptimization(Method* dependee);
 274   static void make_marked_nmethods_not_entrant();
 275 
 276   // Flushing and deoptimization
 277   static void flush_dependents_on(InstanceKlass* dependee);
 278 
 279   // RedefineClasses support
 280   // Flushing and deoptimization in case of evolution
 281   static void mark_for_evol_deoptimization(InstanceKlass* dependee);
 282   static int  mark_dependents_for_evol_deoptimization();
 283   static void mark_all_nmethods_for_evol_deoptimization();
 284   static void flush_evol_dependents();
 285   static void old_nmethods_do(MetadataClosure* f);
 286   static void unregister_old_nmethod(CompiledMethod* c);
 287 
 288   // Support for fullspeed debugging
 289   static void flush_dependents_on_method(const methodHandle& dependee);
 290 
 291   // tells how many nmethods have dependencies
 292   static int number_of_nmethods_with_dependencies();
 293 
 294   static int get_codemem_full_count(int code_blob_type) {
 295     CodeHeap* heap = get_code_heap(code_blob_type);
 296     return (heap != NULL) ? heap->full_count() : 0;
 297   }
 298 
 299   // CodeHeap State Analytics.
 300   // interface methods for CodeHeap printing, called by CompileBroker
 301   static void aggregate(outputStream *out, size_t granularity);
 302   static void discard(outputStream *out);
 303   static void print_usedSpace(outputStream *out);
 304   static void print_freeSpace(outputStream *out);
 305   static void print_count(outputStream *out);
 306   static void print_space(outputStream *out);
 307   static void print_age(outputStream *out);
 308   static void print_names(outputStream *out);
 309 };
 310 
 311 
 312 // Iterator to iterate over nmethods in the CodeCache.
 313 template <class T, class Filter> class CodeBlobIterator : public StackObj {
 314  public:
 315   enum LivenessFilter { all_blobs, only_alive, only_alive_and_not_unloading };
 316 
 317  private:
 318   CodeBlob* _code_blob;   // Current CodeBlob
 319   GrowableArrayIterator<CodeHeap*> _heap;
 320   GrowableArrayIterator<CodeHeap*> _end;
 321   bool _only_alive;
 322   bool _only_not_unloading;
 323 
 324  public:
 325   CodeBlobIterator(LivenessFilter filter, T* nm = NULL)
 326     : _only_alive(filter == only_alive || filter == only_alive_and_not_unloading),
 327       _only_not_unloading(filter == only_alive_and_not_unloading)
 328   {
 329     if (Filter::heaps() == NULL) {
 330       return;
 331     }
 332     _heap = Filter::heaps()->begin();
 333     _end = Filter::heaps()->end();
 334     // If set to NULL, initialized by first call to next()
 335     _code_blob = (CodeBlob*)nm;
 336     if (nm != NULL) {
 337       while(!(*_heap)->contains_blob(_code_blob)) {
 338         ++_heap;
 339       }
 340       assert((*_heap)->contains_blob(_code_blob), "match not found");
 341     }
 342   }
 343 
 344   // Advance iterator to next blob
 345   bool next() {
 346     assert_locked_or_safepoint(CodeCache_lock);
 347 
 348     for (;;) {
 349       // Walk through heaps as required
 350       if (!next_blob()) {
 351         if (_heap == _end) {
 352           return false;
 353         }
 354         ++_heap;
 355         continue;
 356       }
 357 
 358       // Filter is_alive as required
 359       if (_only_alive && !_code_blob->is_alive()) {
 360         continue;
 361       }
 362 
 363       // Filter is_unloading as required
 364       if (_only_not_unloading) {
 365         CompiledMethod* cm = _code_blob->as_compiled_method_or_null();
 366         if (cm != NULL && cm->is_unloading()) {
 367           continue;
 368         }
 369       }
 370 
 371       return true;
 372     }
 373   }
 374 
 375   bool end()  const { return _code_blob == NULL; }
 376   T* method() const { return (T*)_code_blob; }
 377 
 378 private:
 379 
 380   // Advance iterator to the next blob in the current code heap
 381   bool next_blob() {
 382     if (_heap == _end) {
 383       return false;
 384     }
 385     CodeHeap *heap = *_heap;
 386     // Get first method CodeBlob
 387     if (_code_blob == NULL) {
 388       _code_blob = CodeCache::first_blob(heap);
 389       if (_code_blob == NULL) {
 390         return false;
 391       } else if (Filter::apply(_code_blob)) {
 392         return true;
 393       }
 394     }
 395     // Search for next method CodeBlob
 396     _code_blob = CodeCache::next_blob(heap, _code_blob);
 397     while (_code_blob != NULL && !Filter::apply(_code_blob)) {
 398       _code_blob = CodeCache::next_blob(heap, _code_blob);
 399     }
 400     return _code_blob != NULL;
 401   }
 402 };
 403 
 404 
 405 struct CompiledMethodFilter {
 406   static bool apply(CodeBlob* cb) { return cb->is_compiled(); }
 407   static const GrowableArray<CodeHeap*>* heaps() { return CodeCache::compiled_heaps(); }
 408 };
 409 
 410 
 411 struct NMethodFilter {
 412   static bool apply(CodeBlob* cb) { return cb->is_nmethod(); }
 413   static const GrowableArray<CodeHeap*>* heaps() { return CodeCache::nmethod_heaps(); }
 414 };
 415 
 416 typedef CodeBlobIterator<CompiledMethod, CompiledMethodFilter> CompiledMethodIterator;
 417 typedef CodeBlobIterator<nmethod, NMethodFilter> NMethodIterator;
 418 
 419 #endif // SHARE_CODE_CODECACHE_HPP