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 mark_scavenge_root_nmethods() PRODUCT_RETURN; 62 static void verify_perm_nmethods(CodeBlobClosure* f_or_null) PRODUCT_RETURN; 63 64 static int _codemem_full_count; 65 static size_t bytes_allocated_in_freelist() { return _heap->allocated_in_freelist(); } 66 static int allocated_segments() { return _heap->allocated_segments(); } 67 static size_t freelist_length() { return _heap->freelist_length(); } 68 69 public: 70 71 // Initialization 72 static void initialize(); 73 74 static void report_codemem_full(); 75 76 // Allocation/administration 77 static CodeBlob* allocate(int size, bool is_critical = false); // allocates a new CodeBlob 78 static void commit(CodeBlob* cb); // called when the allocated CodeBlob has been filled 79 static int alignment_unit(); // guaranteed alignment of all CodeBlobs 80 static int alignment_offset(); // guaranteed offset of first CodeBlob byte within alignment unit (i.e., allocation header) 81 static void free(CodeBlob* cb); // frees a CodeBlob 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 print_memory_overhead(); 154 static void verify(); // verifies the code cache 155 static void print_trace(const char* event, CodeBlob* cb, int size = 0) PRODUCT_RETURN; 156 static void print_summary(outputStream* st, bool detailed = true); // Prints a summary of the code cache usage 157 static void log_state(outputStream* st); 158 159 // The full limits of the codeCache 160 static address low_bound() { return (address) _heap->low_boundary(); } 161 static address high_bound() { return (address) _heap->high_boundary(); } 162 static address high() { return (address) _heap->high(); } 163 164 // Profiling 165 static address first_address(); // first address used for CodeBlobs 166 static address last_address(); // last address used for CodeBlobs 167 static size_t capacity() { return _heap->capacity(); } 168 static size_t max_capacity() { return _heap->max_capacity(); } 169 static size_t unallocated_capacity() { return _heap->unallocated_capacity(); } 170 static double reverse_free_ratio(); 171 172 static bool needs_cache_clean() { return _needs_cache_clean; } 173 static void set_needs_cache_clean(bool v) { _needs_cache_clean = v; } 174 static void clear_inline_caches(); // clear all inline caches 175 176 // Deoptimization 177 static int mark_for_deoptimization(DepChange& changes); 178 #ifdef HOTSWAP 179 static int mark_for_evol_deoptimization(instanceKlassHandle dependee); 180 #endif // HOTSWAP 181 182 static void mark_all_nmethods_for_deoptimization(); 183 static int mark_for_deoptimization(Method* dependee); 184 static void make_marked_nmethods_zombies(); 185 static void make_marked_nmethods_not_entrant(); 186 187 // tells how many nmethods have dependencies 188 static int number_of_nmethods_with_dependencies(); 189 190 static int get_codemem_full_count() { return _codemem_full_count; } 191 }; 192 193 #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 // 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 |