1 /* 2 * Copyright (c) 1997, 2015, 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 "code/nmethod.hpp" 30 #include "memory/allocation.hpp" 31 #include "memory/heap.hpp" 32 #include "oops/instanceKlass.hpp" 33 #include "oops/oopsHierarchy.hpp" 34 #include "runtime/mutexLocker.hpp" 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-nmethods: Non-nmethods 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 // In the rare case of the non-nmethod code heap getting full, non-nmethod code 52 // will be stored in the non-profiled code heap as a fallback solution. 53 // 54 // Depending on the availability of compilers and TieredCompilation there 55 // may be fewer heaps. The size of the code heaps depends on the values of 56 // ReservedCodeCacheSize, NonProfiledCodeHeapSize and ProfiledCodeHeapSize 57 // (see CodeCache::heap_available(..) and CodeCache::initialize_heaps(..) 58 // for details). 59 // 60 // Code cache segmentation is controlled by the flag SegmentedCodeCache. 61 // If turned off, all code types are stored in a single code heap. By default 62 // code cache segmentation is turned on if TieredCompilation is enabled and 63 // ReservedCodeCacheSize >= 240 MB. 64 // 65 // All methods of the CodeCache accepting a CodeBlobType only apply to 66 // CodeBlobs of the given type. For example, iteration over the 67 // CodeBlobs of a specific type can be done by using CodeCache::first_blob(..) 68 // and CodeCache::next_blob(..) and providing the corresponding CodeBlobType. 69 // 70 // IMPORTANT: If you add new CodeHeaps to the code cache or change the 71 // existing ones, make sure to adapt the dtrace scripts (jhelper.d) for 72 // Solaris and BSD. 73 74 class OopClosure; 75 class DepChange; 76 77 class CodeCache : AllStatic { 78 friend class VMStructs; 79 friend class NMethodIterator; 80 friend class WhiteBox; 81 private: 82 // CodeHeaps of the cache 83 static GrowableArray<CodeHeap*>* _heaps; 84 85 static address _low_bound; // Lower bound of CodeHeap addresses 86 static address _high_bound; // Upper bound of CodeHeap addresses 87 static int _number_of_blobs; // Total number of CodeBlobs in the cache 88 static int _number_of_adapters; // Total number of Adapters in the cache 89 static int _number_of_nmethods; // Total number of nmethods in the cache 90 static int _number_of_nmethods_with_dependencies; // Total number of nmethods with dependencies 91 static bool _needs_cache_clean; // True if inline caches of the nmethods needs to be flushed 92 static nmethod* _scavenge_root_nmethods; // linked via nm->scavenge_root_link() 93 static int _codemem_full_count; // Number of times a CodeHeap in the cache was full 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, int code_blob_type); 102 static CodeHeap* get_code_heap(const CodeBlob* cb); // Returns the CodeHeap for the given CodeBlob 103 static CodeHeap* get_code_heap(int code_blob_type); // Returns the CodeHeap for the given CodeBlobType 104 // Returns the name of the VM option to set the size of the corresponding CodeHeap 105 static const char* get_code_heap_flag_name(int code_blob_type); 106 static bool heap_available(int code_blob_type); // Returns true if an own CodeHeap for the given CodeBlobType is available 107 static size_t heap_alignment(); // Returns the alignment of the CodeHeaps in bytes 108 static ReservedCodeSpace reserve_heap_memory(size_t size); // Reserves one continuous chunk of memory for the CodeHeaps 109 110 // Iteration 111 static CodeBlob* first_blob(CodeHeap* heap); // Returns the first CodeBlob on the given CodeHeap 112 static CodeBlob* first_blob(int code_blob_type); // Returns the first CodeBlob of the given type 113 static CodeBlob* next_blob(CodeHeap* heap, CodeBlob* cb); // Returns the first alive CodeBlob on the given CodeHeap 114 static CodeBlob* next_blob(CodeBlob* cb); // Returns the next CodeBlob of the given type succeeding the given CodeBlob 115 116 static size_t bytes_allocated_in_freelists(); 117 static int allocated_segments(); 118 static size_t freelists_length(); 119 120 public: 121 // Initialization 122 static void initialize(); 123 124 // Allocation/administration 125 static CodeBlob* allocate(int size, int code_blob_type, bool strict = false); // allocates a new CodeBlob 126 static void commit(CodeBlob* cb); // called when the allocated CodeBlob has been filled 127 static int alignment_unit(); // guaranteed alignment of all CodeBlobs 128 static int alignment_offset(); // guaranteed offset of first CodeBlob byte within alignment unit (i.e., allocation header) 129 static void free(CodeBlob* cb); // frees a CodeBlob 130 static bool contains(void *p); // returns whether p is included 131 static void blobs_do(void f(CodeBlob* cb)); // iterates over all CodeBlobs 132 static void blobs_do(CodeBlobClosure* f); // iterates over all CodeBlobs 133 static void nmethods_do(void f(nmethod* nm)); // iterates over all nmethods 134 static void alive_nmethods_do(void f(nmethod* nm)); // iterates over all alive nmethods 135 136 // Lookup 137 static CodeBlob* find_blob(void* start); // Returns the CodeBlob containing the given address 138 static CodeBlob* find_blob_unsafe(void* start); // Same as find_blob but does not fail if looking up a zombie method 139 static nmethod* find_nmethod(void* start); // Returns the nmethod containing the given address 140 141 static int nof_blobs() { return _number_of_blobs; } // Returns the total number of CodeBlobs in the cache 142 static int nof_adapters() { return _number_of_adapters; } // Returns the total number of Adapters in the cache 143 static int nof_nmethods() { return _number_of_nmethods; } // Returns the total number of nmethods in the cache 144 145 // GC support 146 static void gc_epilogue(); 147 static void gc_prologue(); 148 static void verify_oops(); 149 // If "unloading_occurred" is true, then unloads (i.e., breaks root links 150 // to) any unmarked codeBlobs in the cache. Sets "marked_for_unloading" 151 // to "true" iff some code got unloaded. 152 static void do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred); 153 static void asserted_non_scavengable_nmethods_do(CodeBlobClosure* f = NULL) PRODUCT_RETURN; 154 static void scavenge_root_nmethods_do(CodeBlobClosure* f); 155 156 static nmethod* scavenge_root_nmethods() { return _scavenge_root_nmethods; } 157 static void set_scavenge_root_nmethods(nmethod* nm) { _scavenge_root_nmethods = nm; } 158 static void add_scavenge_root_nmethod(nmethod* nm); 159 static void drop_scavenge_root_nmethod(nmethod* nm); 160 static void prune_scavenge_root_nmethods(); 161 162 // Printing/debugging 163 static void print(); // prints summary 164 static void print_internals(); 165 static void print_memory_overhead(); 166 static void verify(); // verifies the code cache 167 static void print_trace(const char* event, CodeBlob* cb, int size = 0) PRODUCT_RETURN; 168 static void print_summary(outputStream* st, bool detailed = true); // Prints a summary of the code cache usage 169 static void log_state(outputStream* st); 170 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"); } 171 static void report_codemem_full(int code_blob_type, bool print); 172 173 // Dcmd (Diagnostic commands) 174 static void print_codelist(outputStream* st); 175 static void print_layout(outputStream* st); 176 177 // The full limits of the codeCache 178 static address low_bound() { return _low_bound; } 179 static address high_bound() { return _high_bound; } 180 181 // Profiling 182 static size_t capacity(); 183 static size_t unallocated_capacity(int code_blob_type); 184 static size_t unallocated_capacity(); 185 static size_t max_capacity(); 186 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 the given nmethod 194 static int get_code_blob_type(nmethod* nm) { 195 return get_code_heap(nm)->code_blob_type(); 196 } 197 198 // Returns the CodeBlobType for the given compilation level 199 static int get_code_blob_type(int comp_level) { 200 if (comp_level == CompLevel_none || 201 comp_level == CompLevel_simple || 202 comp_level == CompLevel_full_optimization) { 203 // Non profiled methods 204 return CodeBlobType::MethodNonProfiled; 205 } else if (comp_level == CompLevel_limited_profile || 206 comp_level == CompLevel_full_profile) { 207 // Profiled methods 208 return CodeBlobType::MethodProfiled; 209 } 210 ShouldNotReachHere(); 211 return 0; 212 } 213 214 static void verify_clean_inline_caches(); 215 static void verify_icholder_relocations(); 216 217 // Deoptimization 218 private: 219 static int mark_for_deoptimization(DepChange& changes); 220 #ifdef HOTSWAP 221 static int mark_for_evol_deoptimization(instanceKlassHandle dependee); 222 #endif // HOTSWAP 223 224 public: 225 static void mark_all_nmethods_for_deoptimization(); 226 static int mark_for_deoptimization(Method* dependee); 227 static void make_marked_nmethods_zombies(); 228 static void make_marked_nmethods_not_entrant(); 229 230 // Flushing and deoptimization 231 static void flush_dependents_on(instanceKlassHandle dependee); 232 static void flush_dependents_on(Handle call_site, Handle method_handle); 233 #ifdef HOTSWAP 234 // Flushing and deoptimization in case of evolution 235 static void flush_evol_dependents_on(instanceKlassHandle dependee); 236 #endif // HOTSWAP 237 // Support for fullspeed debugging 238 static void flush_dependents_on_method(methodHandle dependee); 239 240 // tells how many nmethods have dependencies 241 static int number_of_nmethods_with_dependencies(); 242 243 static int get_codemem_full_count() { return _codemem_full_count; } 244 }; 245 246 247 // Iterator to iterate over nmethods in the CodeCache. 248 class NMethodIterator : public StackObj { 249 private: 250 CodeBlob* _code_blob; // Current CodeBlob 251 int _code_blob_type; // Refers to current CodeHeap 252 253 public: 254 NMethodIterator() { 255 initialize(NULL); // Set to NULL, initialized by first call to next() 256 } 257 258 NMethodIterator(nmethod* nm) { 259 initialize(nm); 260 } 261 262 // Advance iterator to next nmethod 263 bool next() { 264 assert_locked_or_safepoint(CodeCache_lock); 265 assert(_code_blob_type < CodeBlobType::NumTypes, "end reached"); 266 267 bool result = next_nmethod(); 268 while (!result && (_code_blob_type < CodeBlobType::MethodProfiled)) { 269 // Advance to next code heap if segmented code cache 270 _code_blob_type++; 271 result = next_nmethod(); 272 } 273 return result; 274 } 275 276 // Advance iterator to next alive nmethod 277 bool next_alive() { 278 bool result = next(); 279 while(result && !_code_blob->is_alive()) { 280 result = next(); 281 } 282 return result; 283 } 284 285 bool end() const { return _code_blob == NULL; } 286 nmethod* method() const { return (nmethod*)_code_blob; } 287 288 private: 289 // Initialize iterator to given nmethod 290 void initialize(nmethod* nm) { 291 _code_blob = (CodeBlob*)nm; 292 if (!SegmentedCodeCache) { 293 // Iterate over all CodeBlobs 294 _code_blob_type = CodeBlobType::All; 295 } else if (nm != NULL) { 296 _code_blob_type = CodeCache::get_code_blob_type(nm); 297 } else { 298 // Only iterate over method code heaps, starting with non-profiled 299 _code_blob_type = CodeBlobType::MethodNonProfiled; 300 } 301 } 302 303 // Advance iterator to the next nmethod in the current code heap 304 bool next_nmethod() { 305 // Get first method CodeBlob 306 if (_code_blob == NULL) { 307 _code_blob = CodeCache::first_blob(_code_blob_type); 308 if (_code_blob == NULL) { 309 return false; 310 } else if (_code_blob->is_nmethod()) { 311 return true; 312 } 313 } 314 // Search for next method CodeBlob 315 _code_blob = CodeCache::next_blob(_code_blob); 316 while (_code_blob != NULL && !_code_blob->is_nmethod()) { 317 _code_blob = CodeCache::next_blob(_code_blob); 318 } 319 return _code_blob != NULL; 320 } 321 }; 322 323 #endif // SHARE_VM_CODE_CODECACHE_HPP