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 // - Each CodeBlob occupies one chunk of memory. 40 // - Like the offset table in oldspace the zone has at table for 41 // locating a method given a addess of an instruction. 42 43 class OopClosure; 44 class DepChange; 45 46 class CodeCache : AllStatic { 47 friend class VMStructs; 48 private: 49 // CodeHeap is malloc()'ed at startup and never deleted during shutdown, 50 // so that the generated assembly code is always there when it's needed. 51 // This may cause memory leak, but is necessary, for now. See 4423824, 52 // 4422213 or 4436291 for details. 53 static CodeHeap * _heap; 54 static int _number_of_blobs; 55 static int _number_of_adapters; 56 static int _number_of_nmethods; 57 static int _number_of_nmethods_with_dependencies; 58 static bool _needs_cache_clean; 59 static nmethod* _scavenge_root_nmethods; // linked via nm->scavenge_root_link() 60 61 static void verify_if_often() PRODUCT_RETURN; 62 63 static void mark_scavenge_root_nmethods() PRODUCT_RETURN; 64 static void verify_perm_nmethods(CodeBlobClosure* f_or_null) PRODUCT_RETURN; 65 66 static int _codemem_full_count; 67 68 public: 69 70 // Initialization 71 static void initialize(); 72 73 static void report_codemem_full(); 74 75 // Allocation/administration 76 static CodeBlob* allocate(int size, bool is_critical = false); // allocates a new CodeBlob 77 static void commit(CodeBlob* cb); // called when the allocated CodeBlob has been filled 78 static int alignment_unit(); // guaranteed alignment of all CodeBlobs 79 static int alignment_offset(); // guaranteed offset of first CodeBlob byte within alignment unit (i.e., allocation header) 80 static void free(CodeBlob* cb); // frees a CodeBlob 81 static void flush(); // flushes all CodeBlobs 82 static bool contains(void *p); // returns whether p is included 83 static void blobs_do(void f(CodeBlob* cb)); // iterates over all CodeBlobs 84 static void blobs_do(CodeBlobClosure* f); // iterates over all CodeBlobs 85 static void nmethods_do(void f(nmethod* nm)); // iterates over all nmethods 86 static void alive_nmethods_do(void f(nmethod* nm)); // iterates over all alive nmethods 87 88 // Lookup 89 static CodeBlob* find_blob(void* start); 90 static nmethod* find_nmethod(void* start); 91 92 // Lookup that does not fail if you lookup a zombie method (if you call this, be sure to know 93 // what you are doing) 94 static CodeBlob* find_blob_unsafe(void* start) { 95 // NMT can walk the stack before code cache is created 96 if (_heap == NULL) return NULL; 97 98 CodeBlob* result = (CodeBlob*)_heap->find_start(start); 99 // this assert is too strong because the heap code will return the 100 // heapblock containing start. That block can often be larger than 101 // the codeBlob itself. If you look up an address that is within 102 // the heapblock but not in the codeBlob you will assert. 103 // 104 // Most things will not lookup such bad addresses. However 105 // AsyncGetCallTrace can see intermediate frames and get that kind 106 // of invalid address and so can a developer using hsfind. 107 // 108 // The more correct answer is to return NULL if blob_contains() returns 109 // false. 110 // assert(result == NULL || result->blob_contains((address)start), "found wrong CodeBlob"); 111 112 if (result != NULL && !result->blob_contains((address)start)) { 113 result = NULL; 114 } 115 return result; 116 } 117 118 // Iteration 119 static CodeBlob* first(); 120 static CodeBlob* next (CodeBlob* cb); 121 static CodeBlob* alive(CodeBlob *cb); 122 static nmethod* alive_nmethod(CodeBlob *cb); 123 static nmethod* first_nmethod(); 124 static nmethod* next_nmethod (CodeBlob* cb); 125 static int nof_blobs() { return _number_of_blobs; } 126 static int nof_adapters() { return _number_of_adapters; } 127 static int nof_nmethods() { return _number_of_nmethods; } 128 129 // GC support 130 static void gc_epilogue(); 131 static void gc_prologue(); 132 static void verify_oops(); 133 // If "unloading_occurred" is true, then unloads (i.e., breaks root links 134 // to) any unmarked codeBlobs in the cache. Sets "marked_for_unloading" 135 // to "true" iff some code got unloaded. 136 static void do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred); 137 static void oops_do(OopClosure* f) { 138 CodeBlobToOopClosure oopc(f, /*do_marking=*/ false); 139 blobs_do(&oopc); 140 } 141 static void asserted_non_scavengable_nmethods_do(CodeBlobClosure* f = NULL) PRODUCT_RETURN; 142 static void scavenge_root_nmethods_do(CodeBlobClosure* f); 143 144 static nmethod* scavenge_root_nmethods() { return _scavenge_root_nmethods; } 145 static void set_scavenge_root_nmethods(nmethod* nm) { _scavenge_root_nmethods = nm; } 146 static void add_scavenge_root_nmethod(nmethod* nm); 147 static void drop_scavenge_root_nmethod(nmethod* nm); 148 static void prune_scavenge_root_nmethods(); 149 150 // Printing/debugging 151 static void print(); // prints summary 152 static void print_internals(); 153 static void verify(); // verifies the code cache 154 static void print_trace(const char* event, CodeBlob* cb, int size = 0) PRODUCT_RETURN; 155 static void print_summary(outputStream* st, bool detailed = true); // Prints a summary of the code cache usage 156 static void log_state(outputStream* st); 157 158 // The full limits of the codeCache 159 static address low_bound() { return (address) _heap->low_boundary(); } 160 static address high_bound() { return (address) _heap->high_boundary(); } 161 static address high() { return (address) _heap->high(); } 162 163 // Profiling 164 static address first_address(); // first address used for CodeBlobs 165 static address last_address(); // last address used for CodeBlobs 166 static size_t capacity() { return _heap->capacity(); } 167 static size_t max_capacity() { return _heap->max_capacity(); } 168 static size_t unallocated_capacity() { return _heap->unallocated_capacity(); } 169 static double reverse_free_ratio(); 170 171 static bool needs_cache_clean() { return _needs_cache_clean; } 172 static void set_needs_cache_clean(bool v) { _needs_cache_clean = v; } 173 static void clear_inline_caches(); // clear all inline caches 174 175 // Deoptimization 176 static int mark_for_deoptimization(DepChange& changes); 177 #ifdef HOTSWAP 178 static int mark_for_evol_deoptimization(instanceKlassHandle dependee); 179 #endif // HOTSWAP 180 181 static void mark_all_nmethods_for_deoptimization(); 182 static int mark_for_deoptimization(Method* dependee); 183 static void make_marked_nmethods_zombies(); 184 static void make_marked_nmethods_not_entrant(); 185 186 // tells how many nmethods have dependencies 187 static int number_of_nmethods_with_dependencies(); 188 189 static int get_codemem_full_count() { return _codemem_full_count; } 190 }; 191 192 #endif // SHARE_VM_CODE_CODECACHE_HPP | 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 |