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 #include "runtime/mutexLocker.hpp" 34 35 36 // The CodeCache implements the code cache for various pieces of generated 37 // code, e.g., compiled java methods, runtime stubs, transition frames, etc. 38 // The entries in the CodeCache are all CodeBlob's. 39 40 // -- Implementation -- 41 // The CodeCache consists of one or more CodeHeaps, each of which contains 42 // CodeBlobs of a specific CodeBlobType. Currently heaps for the following 43 // types are available: 44 // - Non-methods: Non-methods like Buffers, Adapters and Runtime Stubs 45 // - Profiled nmethods: nmethods that are profiled, i.e., those 46 // executed at level 2 or 3 47 // - Non-Profiled nmethods: nmethods that are not profiled, i.e., those 48 // executed at level 1 or 4 and native methods 49 // - All: Used for code of all types if code cache segmentation is disabled. 50 // 51 // Depending on the availability of compilers and TieredCompilation there 52 // may be fewer heaps. The size of the code heaps depends on the values of 53 // ReservedCodeCacheSize, NonProfiledCodeHeapSize and ProfiledCodeHeapSize 54 // (see CodeCache::heap_available(..) and CodeCache::initialize_heaps(..) 55 // for details). 56 // 57 // Code cache segmentation is controlled by the flag SegmentedCodeCache. 58 // If turned off, all code types are stored in a single code heap. By default 59 // code cache segmentation is turned on if TieredCompilation is enabled and 60 // ReservedCodeCacheSize >= 240 MB. 61 // 62 // All methods of the CodeCache accepting a CodeBlobType only apply to 63 // CodeBlobs of the given type. For example, iteration over the 64 // CodeBlobs of a specific type can be done by using CodeCache::first_blob(..) 65 // and CodeCache::next_blob(..) and providing the corresponding CodeBlobType. 66 // 67 // IMPORTANT: If you add new CodeHeaps to the code cache or change the 68 // existing ones, make sure to adapt the dtrace scripts (jhelper.d) for 69 // Solaris and BSD. 70 71 class OopClosure; 72 class DepChange; 73 74 class CodeCache : AllStatic { 75 friend class VMStructs; 76 private: 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 int _codemem_full_count; // Number of times a CodeHeap in the cache was full 89 90 static void mark_scavenge_root_nmethods() PRODUCT_RETURN; 91 static void verify_perm_nmethods(CodeBlobClosure* f_or_null) PRODUCT_RETURN; 92 93 // CodeHeap management 94 static void initialize_heaps(); // Initializes the CodeHeaps 95 // Creates a new heap with the given name and size, containing CodeBlobs of the given type 96 static void add_heap(ReservedSpace rs, const char* name, size_t size_initial, int code_blob_type); 97 static CodeHeap* get_code_heap(int code_blob_type); // Returns the CodeHeap for the given CodeBlobType 98 static bool heap_available(int code_blob_type); // Returns true if a CodeHeap for the given CodeBlobType is available 99 static ReservedCodeSpace reserve_heap_memory(size_t size); // Reserves one continuous chunk of memory for the CodeHeaps 100 101 // Iteration 102 static CodeBlob* first_blob(CodeHeap* heap); // Returns the first CodeBlob on the given CodeHeap 103 static CodeBlob* next_blob(CodeHeap* heap, CodeBlob* cb); // Returns the first alive CodeBlob on the given CodeHeap 104 static CodeBlob* first_alive_blob(CodeHeap* heap); // Returns the next CodeBlob on the given CodeHeap succeeding the given CodeBlob 105 static CodeBlob* next_alive_blob(CodeHeap* heap, CodeBlob* cb); // Returns the next alive CodeBlob on the given CodeHeap succeeding the given CodeBlob 106 107 static size_t bytes_allocated_in_freelists(); 108 static int allocated_segments(); 109 static size_t freelists_length(); 110 111 public: 112 // Initialization 113 static void initialize(); 114 115 // Allocation/administration 116 static CodeBlob* allocate(int size, int code_blob_type, bool is_critical = false); // allocates a new CodeBlob 117 static void commit(CodeBlob* cb); // called when the allocated CodeBlob has been filled 118 static int alignment_unit(); // guaranteed alignment of all CodeBlobs 119 static int alignment_offset(); // guaranteed offset of first CodeBlob byte within alignment unit (i.e., allocation header) 120 static void free(CodeBlob* cb, int code_blob_type); // frees a CodeBlob 121 static bool contains(void *p); // returns whether p is included 122 static void blobs_do(void f(CodeBlob* cb)); // iterates over all CodeBlobs 123 static void blobs_do(CodeBlobClosure* f); // iterates over all CodeBlobs 124 static void nmethods_do(void f(nmethod* nm)); // iterates over all nmethods 125 static void alive_nmethods_do(void f(nmethod* nm)); // iterates over all alive nmethods 126 127 // Lookup 128 static CodeBlob* find_blob(void* start); // Returns the CodeBlob containing the given address 129 static CodeBlob* find_blob_unsafe(void* start); // Same as find_blob but does not fail if looking up a zombie method 130 static nmethod* find_nmethod(void* start); // Returns the nmethod containing the given address 131 132 // Iteration 133 // Returns the first CodeBlob of the given type 134 static CodeBlob* first_blob(int code_blob_type) { return first_blob(get_code_heap(code_blob_type)); } 135 // Returns the next CodeBlob of the given type succeeding the given CodeBlob 136 static CodeBlob* next_blob(CodeBlob* cb, int code_blob_type) { return next_blob(get_code_heap(code_blob_type), cb); } 137 138 static int nof_blobs() { return _number_of_blobs; } // Returns the total number of CodeBlobs in the cache 139 static int nof_adapters() { return _number_of_adapters; } // Returns the total number of Adapters in the cache 140 static int nof_nmethods() { return _number_of_nmethods; } // Returns the total number of nmethods in the cache 141 142 // GC support 143 static void gc_epilogue(); 144 static void gc_prologue(); 145 static void verify_oops(); 146 // If "unloading_occurred" is true, then unloads (i.e., breaks root links 147 // to) any unmarked codeBlobs in the cache. Sets "marked_for_unloading" 148 // to "true" iff some code got unloaded. 149 static void do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred); 150 static void oops_do(OopClosure* f) { 151 CodeBlobToOopClosure oopc(f, /*do_marking=*/ false); 152 blobs_do(&oopc); 153 } 154 static void asserted_non_scavengable_nmethods_do(CodeBlobClosure* f = NULL) PRODUCT_RETURN; 155 static void scavenge_root_nmethods_do(CodeBlobClosure* f); 156 157 static nmethod* scavenge_root_nmethods() { return _scavenge_root_nmethods; } 158 static void set_scavenge_root_nmethods(nmethod* nm) { _scavenge_root_nmethods = nm; } 159 static void add_scavenge_root_nmethod(nmethod* nm); 160 static void drop_scavenge_root_nmethod(nmethod* nm); 161 static void prune_scavenge_root_nmethods(); 162 163 // Printing/debugging 164 static void print(); // prints summary 165 static void print_internals(); 166 static void print_memory_overhead(); 167 static void verify(); // verifies the code cache 168 static void print_trace(const char* event, CodeBlob* cb, int size = 0) PRODUCT_RETURN; 169 static void print_summary(outputStream* st, bool detailed = true); // Prints a summary of the code cache usage 170 static void log_state(outputStream* st); 171 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"); } 172 static void report_codemem_full(int code_blob_type, bool print); 173 174 // The full limits of the codeCache 175 static address low_bound() { return _low_bound; } 176 static address high_bound() { return _high_bound; } 177 178 // Profiling 179 static size_t capacity(int code_blob_type) { return heap_available(code_blob_type) ? get_code_heap(code_blob_type)->capacity() : 0; } 180 static size_t capacity(); 181 static size_t unallocated_capacity(int code_blob_type) { return heap_available(code_blob_type) ? get_code_heap(code_blob_type)->unallocated_capacity() : 0; } 182 static size_t unallocated_capacity(); 183 static size_t max_capacity(int code_blob_type) { return heap_available(code_blob_type) ? get_code_heap(code_blob_type)->max_capacity() : 0; } 184 static size_t max_capacity(); 185 186 static bool is_full(int* code_blob_type); 187 static double reverse_free_ratio(int code_blob_type); 188 189 static bool needs_cache_clean() { return _needs_cache_clean; } 190 static void set_needs_cache_clean(bool v) { _needs_cache_clean = v; } 191 static void clear_inline_caches(); // clear all inline caches 192 193 // Returns the CodeBlobType for nmethods of the given compilation level 194 static int get_code_blob_type(int comp_level) { 195 if (comp_level == CompLevel_none || 196 comp_level == CompLevel_simple || 197 comp_level == CompLevel_full_optimization) { 198 // Non profiled methods 199 return CodeBlobType::MethodNonProfiled; 200 } else if (comp_level == CompLevel_limited_profile || 201 comp_level == CompLevel_full_profile) { 202 // Profiled methods 203 return CodeBlobType::MethodProfiled; 204 } 205 ShouldNotReachHere(); 206 return 0; 207 } 208 209 // Deoptimization 210 static int mark_for_deoptimization(DepChange& changes); 211 #ifdef HOTSWAP 212 static int mark_for_evol_deoptimization(instanceKlassHandle dependee); 213 #endif // HOTSWAP 214 215 static void mark_all_nmethods_for_deoptimization(); 216 static int mark_for_deoptimization(Method* dependee); 217 static void make_marked_nmethods_zombies(); 218 static void make_marked_nmethods_not_entrant(); 219 220 // tells how many nmethods have dependencies 221 static int number_of_nmethods_with_dependencies(); 222 223 static int get_codemem_full_count() { return _codemem_full_count; } 224 }; 225 226 227 // Iterator to iterate over nmethods in the CodeCache. 228 class NMethodIterator : public StackObj { 229 private: 230 int _code_blob_type; // Refers to current CodeHeap 231 CodeBlob* _code_blob; // Current CodeBlob 232 233 public: 234 NMethodIterator() { 235 if (SegmentedCodeCache) { 236 // Only iterate over method code heaps 237 _code_blob_type = CodeBlobType::MethodNonProfiled; 238 } else { 239 // Iterate over all CodeBlobs 240 _code_blob_type = CodeBlobType::All; 241 } 242 // Initialize to NULL, first call to next() sets _code_blob 243 _code_blob = NULL; 244 } 245 246 // Advance iterator to next nmethod 247 bool next() { 248 assert_locked_or_safepoint(CodeCache_lock); 249 assert(_code_blob_type < CodeBlobType::NumTypes, "end reached"); 250 251 if (SegmentedCodeCache) { 252 // Code cache is segmented, iterate over all method heaps 253 // Get first CodeBlob 254 if (_code_blob == NULL) { 255 _code_blob = CodeCache::first_blob(_code_blob_type); 256 return _code_blob != NULL; 257 } 258 // Get next CodeBlob 259 _code_blob = CodeCache::next_blob(_code_blob, _code_blob_type); 260 while (_code_blob == NULL && _code_blob_type < CodeBlobType::MethodProfiled) { 261 // Advance to next CodeBlobType if CodeBlob is NULL 262 _code_blob_type++; 263 _code_blob = CodeCache::first_blob(_code_blob_type); 264 } 265 } else { 266 // Code cache is not segmented, search for method code blobs 267 // Get first method CodeBlob 268 if (_code_blob == NULL) { 269 _code_blob = CodeCache::first_blob(_code_blob_type); 270 if (_code_blob->is_nmethod()) { 271 return true; 272 } 273 } 274 // Search for next method CodeBlob 275 _code_blob = CodeCache::next_blob(_code_blob, _code_blob_type); 276 while (_code_blob != NULL && !_code_blob->is_nmethod()) { 277 _code_blob = CodeCache::next_blob(_code_blob, _code_blob_type); 278 } 279 } 280 // End reached? 281 return _code_blob != NULL; 282 } 283 284 bool end() const { return _code_blob == NULL; } 285 nmethod* method() const { return (nmethod*)_code_blob; } 286 }; 287 288 #endif // SHARE_VM_CODE_CODECACHE_HPP