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 //    executed at level 2 or 3
  45 //  - Non-Profiled nmethods: nmethods that are not profiled, i.e., those
  46 //    executed at level 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 
  65 class CodeCache : AllStatic {
  66   friend class VMStructs;
  67  private:
  68   // Predicate returning true for all method heaps
  69   class IsMethodPredicate {
  70     public:
  71       bool operator()(const CodeHeap* heap) {
  72         return heap->accepts(CodeBlobType::MethodProfiled)
  73             || heap->accepts(CodeBlobType::MethodNonProfiled);
  74       }
  75   };
  76 
  77   // CodeHeaps of the cache
  78   static GrowableArray<CodeHeap*>* _heaps;
  79 
  80   static address _low_bound;                            // Lower bound of CodeHeap addresses
  81   static address _high_bound;                           // Upper bound of CodeHeap addresses
  82   static int _number_of_blobs;                          // Total number of CodeBlobs in the cache
  83   static int _number_of_adapters;                       // Total number of Adapters in the cache
  84   static int _number_of_nmethods;                       // Total number of nmethods in the cache
  85   static int _number_of_nmethods_with_dependencies;     // Total number of nmethods with dependencies
  86   static bool _needs_cache_clean;                       // True if inline caches of the nmethods needs to be flushed
  87   static nmethod* _scavenge_root_nmethods;              // linked via nm->scavenge_root_link()
  88   static nmethod* _saved_nmethods;                      // Linked list of speculatively disconnected nmethods.
  89   static int _codemem_full_count;                       // Number of times a CodeHeap in the cache was full
  90 
  91   static void mark_scavenge_root_nmethods() PRODUCT_RETURN;
  92   static void verify_perm_nmethods(CodeBlobClosure* f_or_null) PRODUCT_RETURN;
  93 
  94   // CodeHeap management
  95   static void initialize_heaps();                             // Initializes the CodeHeaps
  96     // Creates a new heap with the given name and size, containing CodeBlobs of the given type
  97   static void add_heap(ReservedSpace rs, const char* name, size_t size_initial, int code_blob_type);
  98   static CodeHeap* get_code_heap(int code_blob_type);         // Returns the CodeHeap for the given CodeBlobType
  99   static bool heap_available(int code_blob_type);             // Returns true if a CodeHeap for the given CodeBlobType is available
 100   static ReservedCodeSpace reserve_heap_memory(size_t size);  // Reserves one continuous chunk of memory for the CodeHeaps
 101 
 102   // Iteration
 103   static CodeBlob* first_blob(CodeHeap* heap);                      // Returns the first CodeBlob on the given CodeHeap
 104   static CodeBlob* next_blob(CodeHeap* heap, CodeBlob* cb);         // Returns the first alive CodeBlob on the given CodeHeap
 105   static CodeBlob* first_alive_blob(CodeHeap* heap);                // Returns the next CodeBlob on the given CodeHeap succeeding the given CodeBlob
 106   static CodeBlob* next_alive_blob(CodeHeap* heap, CodeBlob* cb);   // Returns the next alive CodeBlob on the given CodeHeap succeeding the given CodeBlob
 107 
 108   static size_t bytes_allocated_in_freelists();
 109   static int    allocated_segments();
 110   static size_t freelists_length();
 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 print_memory_overhead();
 173   static void verify();                          // verifies the code cache
 174   static void print_trace(const char* event, CodeBlob* cb, int size = 0) PRODUCT_RETURN;
 175   static void print_summary(outputStream* st, bool detailed = true); // Prints a summary of the code cache usage
 176   static void log_state(outputStream* st);
 177   static const char* get_heap_name(int code_blob_type)  { return (heap_available(code_blob_type) ? get_code_heap(code_blob_type)->name() : "Unused"); }
 178   static void report_codemem_full(int code_blob_type, bool print);
 179 
 180   // The full limits of the codeCache
 181   static address low_bound()                          { return _low_bound; }
 182   static address high_bound()                         { return _high_bound; }

 183 
 184   // Profiling
 185   static size_t capacity(int code_blob_type)             { return heap_available(code_blob_type) ? get_code_heap(code_blob_type)->capacity() : 0; }
 186   static size_t capacity();
 187   static size_t unallocated_capacity(int code_blob_type) { return heap_available(code_blob_type) ? get_code_heap(code_blob_type)->unallocated_capacity() : 0; }
 188   static size_t unallocated_capacity();
 189   static size_t max_capacity(int code_blob_type)         { return heap_available(code_blob_type) ? get_code_heap(code_blob_type)->max_capacity() : 0; }
 190   static size_t max_capacity();
 191 
 192   static bool   is_full(int code_blob_type)              { return heap_available(code_blob_type) && (unallocated_capacity(code_blob_type) < CodeCacheMinimumFreeSpace); }
 193   static double reverse_free_ratio(int code_blob_type);
 194 
 195   static bool needs_cache_clean()                     { return _needs_cache_clean; }
 196   static void set_needs_cache_clean(bool v)           { _needs_cache_clean = v;    }
 197   static void clear_inline_caches();                  // clear all inline caches
 198 
 199   // Returns the CodeBlobType for nmethods of the given compilation level
 200   static int get_code_blob_type(int comp_level) {
 201     if (comp_level == CompLevel_none ||
 202         comp_level == CompLevel_simple ||
 203         comp_level == CompLevel_full_optimization) {
 204       // Non profiled methods
 205       return CodeBlobType::MethodNonProfiled;
 206     } else if (comp_level == CompLevel_limited_profile ||
 207                comp_level == CompLevel_full_profile) {
 208       // Profiled methods
 209       return CodeBlobType::MethodProfiled;
 210     }
 211     ShouldNotReachHere();
 212     return 0;
 213   }
 214 
 215   // Deoptimization
 216   static int  mark_for_deoptimization(DepChange& changes);
 217 #ifdef HOTSWAP
 218   static int  mark_for_evol_deoptimization(instanceKlassHandle dependee);
 219 #endif // HOTSWAP
 220 
 221   static void mark_all_nmethods_for_deoptimization();
 222   static int  mark_for_deoptimization(Method* dependee);
 223   static void make_marked_nmethods_zombies();
 224   static void make_marked_nmethods_not_entrant();
 225 
 226   // tells how many nmethods have dependencies
 227   static int number_of_nmethods_with_dependencies();
 228 
 229   static int get_codemem_full_count() { return _codemem_full_count; }
 230 };
 231 
 232 #endif // SHARE_VM_CODE_CODECACHE_HPP
--- EOF ---