1 /*
   2  * Copyright (c) 1997, 2013, 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_VM_CODE_CODECACHE_HPP
  26 #define SHARE_VM_CODE_CODECACHE_HPP
  27 
  28 #include "code/codeBlob.hpp"
  29 #include "memory/allocation.hpp"
  30 #include "memory/heap.hpp"
  31 #include "oops/instanceKlass.hpp"
  32 #include "oops/oopsHierarchy.hpp"
  33 
  34 // The CodeCache implements the code cache for various pieces of generated
  35 // code, e.g., compiled java methods, runtime stubs, transition frames, etc.
  36 // The entries in the CodeCache are all CodeBlob's.
  37 
  38 // -- Implementation --
  39 // The CodeCache consists of multiple CodeHeaps, each of which contains
  40 // CodeBlobs of a specific CodeBlobType. Currently heaps for the following
  41 // types are available:
  42 //  - Non-methods: Non-methods like Buffers, Adapters and Runtime Stubs
  43 //  - Profiled nmethods: nmethods that are profiled, i.e., those
  44 //    compiled at tier 2 or 3
  45 //  - Non-Profiled nmethods: nmethods that are not profiled, i.e., those
  46 //    compiled at tier 1 or 4 and native methods
  47 //
  48 // Depending on the availability of compilers and TieredCompilation being
  49 // deactivated there may be fewer heaps. The size of the heaps depends on
  50 // the values of ReservedCodeCacheSize, NonProfiledCodeHeapSize and
  51 // ProfiledCodeHeapSize (see CodeCache::initialize_heaps for details).
  52 //
  53 // All methods of the CodeCache accepting a CodeBlobType only apply to
  54 // CodeBlobs of the given type. For example, iteration over the
  55 // CodeBlobs of a specific type can be done by using CodeCache::first_blob
  56 // and CodeCache::next_blob and providing the corresponding CodeBlobType.
  57 //
  58 // IMPORTANT: If you add new CodeHeaps to the code cache or change the
  59 // existing ones, make sure to adapt the dtrace scripts (jhelper.d) for
  60 // Solaris and BSD.
  61 
  62 class OopClosure;
  63 class DepChange;
  64 class HeapConfiguration;
  65 
  66 class CodeCache : AllStatic {
  67   friend class VMStructs;
  68  private:
  69   // Predicate returning true for all method heaps
  70   class IsMethodPredicate {
  71     public:
  72       bool operator()(const CodeHeap* heap) {
  73         return heap->accepts(CodeBlobType::MethodProfiled)
  74             || heap->accepts(CodeBlobType::MethodNonProfiled);
  75       }
  76   };
  77 
  78   // CodeHeaps of the cache
  79   static GrowableArray<CodeHeap*>* _heaps;
  80 
  81   static address _low_bound;                            // Lower bound of CodeHeap addresses
  82   static address _high_bound;                           // Upper bound of CodeHeap addresses
  83   static int _number_of_blobs;                          // Total number of CodeBlobs in the cache
  84   static int _number_of_adapters;                       // Total number of Adapters in the cache
  85   static int _number_of_nmethods;                       // Total number of nmethods in the cache
  86   static int _number_of_nmethods_with_dependencies;     // Total number of nmethods with dependencies
  87   static bool _needs_cache_clean;                       // True if inline caches of the nmethods needs to be flushed
  88   static nmethod* _scavenge_root_nmethods;              // linked via nm->scavenge_root_link()
  89   static nmethod* _saved_nmethods;                      // Linked list of speculatively disconnected nmethods.
  90   static int _codemem_full_count;                       // Number of times a CodeHeap in the cache was full
  91 
  92   // CodeHeap verification
  93   static void verify_if_often() PRODUCT_RETURN;
  94 
  95   static void mark_scavenge_root_nmethods() PRODUCT_RETURN;
  96   static void verify_perm_nmethods(CodeBlobClosure* f_or_null) PRODUCT_RETURN;
  97 
  98   // CodeHeap management
  99   static void initialize_heaps();                             // Initializes the CodeHeaps
 100     // Creates a new heap with the given name and size, containing CodeBlobs of the given type
 101   static void add_heap(ReservedSpace rs, const char* name, size_t size_initial, int code_blob_type);
 102   static CodeHeap* get_code_heap(int code_blob_type);         // Returns the CodeHeap for the given CodeBlobType
 103   static bool heap_available(int code_blob_type);             // Returns true if a CodeHeap for the given CodeBlobType is available
 104   static ReservedCodeSpace reserve_heap_memory(size_t size);  // Reserves one continuous chunk of memory for the CodeHeaps
 105 
 106   // Iteration
 107   static CodeBlob* first_blob(CodeHeap* heap);                      // Returns the first CodeBlob on the given CodeHeap
 108   static CodeBlob* next_blob(CodeHeap* heap, CodeBlob* cb);         // Returns the first alive CodeBlob on the given CodeHeap
 109   static CodeBlob* first_alive_blob(CodeHeap* heap);                // Returns the next CodeBlob on the given CodeHeap succeeding the given CodeBlob
 110   static CodeBlob* next_alive_blob(CodeHeap* heap, CodeBlob* cb);   // Returns the next alive CodeBlob on the given CodeHeap succeeding the given CodeBlob
 111 
 112  public:
 113   // Initialization
 114   static void initialize();
 115 
 116   // Allocation/administration
 117   static CodeBlob* allocate(int size, int code_blob_type, bool is_critical = false); // allocates a new CodeBlob
 118   static void commit(CodeBlob* cb);                     // called when the allocated CodeBlob has been filled
 119   static int  alignment_unit();                         // guaranteed alignment of all CodeBlobs
 120   static int  alignment_offset();                       // guaranteed offset of first CodeBlob byte within alignment unit (i.e., allocation header)
 121   static void free(CodeBlob* cb, int code_blob_type);   // frees a CodeBlob
 122   static bool contains(void *p);                        // returns whether p is included
 123   static void blobs_do(void f(CodeBlob* cb));           // iterates over all CodeBlobs
 124   static void blobs_do(CodeBlobClosure* f);             // iterates over all CodeBlobs
 125   static void nmethods_do(void f(nmethod* nm));         // iterates over all nmethods
 126   static void alive_nmethods_do(void f(nmethod* nm));   // iterates over all alive nmethods
 127 
 128   // Lookup
 129   static CodeBlob* find_blob(void* start);              // Returns the CodeBlob containing the given address
 130   static CodeBlob* find_blob_unsafe(void* start);       // Same as find_blob but does not fail if looking up a zombie method
 131   static nmethod*  find_nmethod(void* start);           // Returns the nmethod containing the given address
 132   static bool      contains_nmethod(nmethod* nm);       // Returns true if the CodeCache contains the given nmethod
 133 
 134   // Iteration
 135   // Returns the first CodeBlob of the given type
 136   static CodeBlob* first_blob(int code_blob_type)                    { return first_blob(get_code_heap(code_blob_type)); }
 137   // Returns the first alive CodeBlob of the given type
 138   static CodeBlob* first_alive_blob(int code_blob_type)              { return first_alive_blob(get_code_heap(code_blob_type)); }
 139   // Returns the next CodeBlob of the given type succeeding the given CodeBlob
 140   static CodeBlob* next_blob(CodeBlob* cb, int code_blob_type)       { return next_blob(get_code_heap(code_blob_type), cb); }
 141   // Returns the next alive CodeBlob of the given type succeeding the given CodeBlob
 142   static CodeBlob* next_alive_blob(CodeBlob* cb, int code_blob_type) { return next_alive_blob(get_code_heap(code_blob_type), cb); }
 143 
 144   static int       nof_blobs()      { return _number_of_blobs; }      // Returns the total number of CodeBlobs in the cache
 145   static int       nof_adapters()   { return _number_of_adapters; }   // Returns the total number of Adapters in the cache
 146   static int       nof_nmethods()   { return _number_of_nmethods; }   // Returns the total number of nmethods in the cache
 147 
 148   // GC support
 149   static void gc_epilogue();
 150   static void gc_prologue();
 151   static void verify_oops();
 152   // If "unloading_occurred" is true, then unloads (i.e., breaks root links
 153   // to) any unmarked codeBlobs in the cache.  Sets "marked_for_unloading"
 154   // to "true" iff some code got unloaded.
 155   static void do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred);
 156   static void oops_do(OopClosure* f) {
 157     CodeBlobToOopClosure oopc(f, /*do_marking=*/ false);
 158     blobs_do(&oopc);
 159   }
 160   static void asserted_non_scavengable_nmethods_do(CodeBlobClosure* f = NULL) PRODUCT_RETURN;
 161   static void scavenge_root_nmethods_do(CodeBlobClosure* f);
 162 
 163   static nmethod* scavenge_root_nmethods()            { return _scavenge_root_nmethods; }
 164   static void set_scavenge_root_nmethods(nmethod* nm) { _scavenge_root_nmethods = nm; }
 165   static void add_scavenge_root_nmethod(nmethod* nm);
 166   static void drop_scavenge_root_nmethod(nmethod* nm);
 167   static void prune_scavenge_root_nmethods();
 168 
 169   // Printing/debugging
 170   static void print();                           // prints summary
 171   static void print_internals();
 172   static void verify();                          // verifies the code cache
 173   static void print_trace(const char* event, CodeBlob* cb, int size = 0) PRODUCT_RETURN;
 174   static void print_summary(outputStream* st, bool detailed = true); // Prints a summary of the code cache usage
 175   static void log_state(outputStream* st);
 176   static const char* get_heap_name(int code_blob_type)  { return (heap_available(code_blob_type) ? get_code_heap(code_blob_type)->name() : "Unused"); }
 177   static void report_codemem_full(int code_blob_type);
 178 
 179   // The full limits of the codeCache
 180   static address low_bound()                          { return _low_bound; }
 181   static address high_bound()                         { return _high_bound; }
 182 
 183   // Profiling
 184   static size_t capacity(int code_blob_type)             { return heap_available(code_blob_type) ? get_code_heap(code_blob_type)->capacity() : 0; }
 185   static size_t capacity();
 186   static size_t unallocated_capacity(int code_blob_type) { return heap_available(code_blob_type) ? get_code_heap(code_blob_type)->unallocated_capacity() : 0; }
 187   static size_t unallocated_capacity();
 188   static size_t max_capacity(int code_blob_type)         { return heap_available(code_blob_type) ? get_code_heap(code_blob_type)->max_capacity() : 0; }
 189   static size_t max_capacity();
 190 
 191   static bool   is_full(int code_blob_type)              { return heap_available(code_blob_type) && (unallocated_capacity(code_blob_type) < CodeCacheMinimumFreeSpace); }
 192   static double reverse_free_ratio(int code_blob_type);
 193 
 194   static bool needs_cache_clean()                     { return _needs_cache_clean; }
 195   static void set_needs_cache_clean(bool v)           { _needs_cache_clean = v;    }
 196 
 197   // Returns the CodeBlobType for nmethods of the given compilation level
 198   static int get_code_blob_type(int comp_level) {
 199     if (comp_level == CompLevel_none ||
 200         comp_level == CompLevel_simple ||
 201         comp_level == CompLevel_full_optimization) {
 202       // Non profiled methods
 203       return CodeBlobType::MethodNonProfiled;
 204     } else if (comp_level == CompLevel_limited_profile ||
 205                comp_level == CompLevel_full_profile) {
 206       // Profiled methods
 207       return CodeBlobType::MethodProfiled;
 208     }
 209     ShouldNotReachHere();
 210     return 0;
 211   }
 212 
 213   // Deoptimization
 214   static int  mark_for_deoptimization(DepChange& changes);
 215 #ifdef HOTSWAP
 216   static int  mark_for_evol_deoptimization(instanceKlassHandle dependee);
 217 #endif // HOTSWAP
 218 
 219   static void mark_all_nmethods_for_deoptimization();
 220   static int  mark_for_deoptimization(Method* dependee);
 221   static void make_marked_nmethods_zombies();
 222   static void make_marked_nmethods_not_entrant();
 223 
 224   // tells how many nmethods have dependencies
 225   static int number_of_nmethods_with_dependencies();
 226 
 227   static int get_codemem_full_count() { return _codemem_full_count; }
 228 };
 229 
 230 #endif // SHARE_VM_CODE_CODECACHE_HPP