rev 13198 : 8183573: Refactor CodeHeap and AOTCodeHeap to devirtualize hot methods
Reviewed-by: rbackman

   1 /*
   2  * Copyright (c) 1997, 2017, 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 KlassDepChange;
  76 
  77 class CodeCache : AllStatic {
  78   friend class VMStructs;
  79   friend class JVMCIVMStructs;
  80   template <class T, class Filter> friend class CodeBlobIterator;
  81   friend class WhiteBox;
  82   friend class CodeCacheLoader;
  83  private:
  84   // CodeHeaps of the cache
  85   static GrowableArray<CodeHeap*>* _heaps;
  86   static GrowableArray<CodeHeap*>* _compiled_heaps;
  87   static GrowableArray<CodeHeap*>* _nmethod_heaps;
  88 
  89   static address _low_bound;                            // Lower bound of CodeHeap addresses
  90   static address _high_bound;                           // Upper bound of CodeHeap addresses
  91   static int _number_of_nmethods_with_dependencies;     // Total number of nmethods with dependencies
  92   static bool _needs_cache_clean;                       // True if inline caches of the nmethods needs to be flushed
  93   static nmethod* _scavenge_root_nmethods;              // linked via nm->scavenge_root_link()
  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   // Check the code heap sizes set by the user via command line
 101   static void check_heap_sizes(size_t non_nmethod_size, size_t profiled_size, size_t non_profiled_size, size_t cache_size, bool all_set);
 102   // Creates a new heap with the given name and size, containing CodeBlobs of the given type
 103   static void add_heap(ReservedSpace rs, const char* name, int code_blob_type);

 104   static CodeHeap* get_code_heap(const CodeBlob* cb);         // Returns the CodeHeap for the given CodeBlob
 105   static CodeHeap* get_code_heap(int code_blob_type);         // Returns the CodeHeap for the given CodeBlobType
 106   // Returns the name of the VM option to set the size of the corresponding CodeHeap
 107   static const char* get_code_heap_flag_name(int code_blob_type);
 108   static size_t heap_alignment();                             // Returns the alignment of the CodeHeaps in bytes
 109   static ReservedCodeSpace reserve_heap_memory(size_t size);  // Reserves one continuous chunk of memory for the CodeHeaps
 110 
 111   // Iteration
 112   static CodeBlob* first_blob(CodeHeap* heap);                // Returns the first CodeBlob on the given CodeHeap
 113   static CodeBlob* first_blob(int code_blob_type);            // Returns the first CodeBlob of the given type
 114   static CodeBlob* next_blob(CodeHeap* heap, CodeBlob* cb);   // Returns the next CodeBlob on the given CodeHeap
 115 
 116   static size_t bytes_allocated_in_freelists();
 117   static int    allocated_segments();
 118   static size_t freelists_length();
 119 
 120   static void set_scavenge_root_nmethods(nmethod* nm) { _scavenge_root_nmethods = nm; }
 121   static void prune_scavenge_root_nmethods();
 122   static void unlink_scavenge_root_nmethod(nmethod* nm, nmethod* prev);
 123 
 124   // Make private to prevent unsafe calls.  Not all CodeBlob*'s are embedded in a CodeHeap.
 125   static bool contains(CodeBlob *p) { fatal("don't call me!"); return false; }
 126 
 127  public:
 128   // Initialization
 129   static void initialize();
 130 
 131   static int code_heap_compare(CodeHeap* const &lhs, CodeHeap* const &rhs);
 132 
 133   static void add_heap(CodeHeap* heap);
 134   static const GrowableArray<CodeHeap*>* heaps() { return _heaps; }
 135   static const GrowableArray<CodeHeap*>* compiled_heaps() { return _compiled_heaps; }
 136   static const GrowableArray<CodeHeap*>* nmethod_heaps() { return _nmethod_heaps; }
 137 
 138   // Allocation/administration
 139   static CodeBlob* allocate(int size, int code_blob_type, int orig_code_blob_type = CodeBlobType::All); // allocates a new CodeBlob
 140   static void commit(CodeBlob* cb);                        // called when the allocated CodeBlob has been filled
 141   static int  alignment_unit();                            // guaranteed alignment of all CodeBlobs
 142   static int  alignment_offset();                          // guaranteed offset of first CodeBlob byte within alignment unit (i.e., allocation header)
 143   static void free(CodeBlob* cb);                          // frees a CodeBlob
 144   static bool contains(void *p);                           // returns whether p is included
 145   static bool contains(nmethod* nm);                       // returns whether nm is included
 146   static void blobs_do(void f(CodeBlob* cb));              // iterates over all CodeBlobs
 147   static void blobs_do(CodeBlobClosure* f);                // iterates over all CodeBlobs
 148   static void nmethods_do(void f(nmethod* nm));            // iterates over all nmethods
 149   static void metadata_do(void f(Metadata* m));            // iterates over metadata in alive nmethods
 150 
 151   // Lookup
 152   static CodeBlob* find_blob(void* start);              // Returns the CodeBlob containing the given address
 153   static CodeBlob* find_blob_unsafe(void* start);       // Same as find_blob but does not fail if looking up a zombie method
 154   static nmethod*  find_nmethod(void* start);           // Returns the nmethod containing the given address
 155   static CompiledMethod* find_compiled(void* start);
 156 
 157   static int       blob_count();                        // Returns the total number of CodeBlobs in the cache
 158   static int       blob_count(int code_blob_type);
 159   static int       adapter_count();                     // Returns the total number of Adapters in the cache
 160   static int       adapter_count(int code_blob_type);
 161   static int       nmethod_count();                     // Returns the total number of nmethods in the cache
 162   static int       nmethod_count(int code_blob_type);
 163 
 164   // GC support
 165   static void gc_epilogue();
 166   static void gc_prologue();
 167   static void verify_oops();
 168   // If "unloading_occurred" is true, then unloads (i.e., breaks root links
 169   // to) any unmarked codeBlobs in the cache.  Sets "marked_for_unloading"
 170   // to "true" iff some code got unloaded.
 171   static void do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred);
 172   static void asserted_non_scavengable_nmethods_do(CodeBlobClosure* f = NULL) PRODUCT_RETURN;
 173 
 174   // Apply f to every live code blob in scavengable nmethods. Prune nmethods
 175   // from the list of scavengable nmethods if f->fix_relocations() and a nmethod
 176   // no longer has scavengable oops.  If f->fix_relocations(), then f must copy
 177   // objects to their new location immediately to avoid fixing nmethods on the
 178   // basis of the old object locations.
 179   static void scavenge_root_nmethods_do(CodeBlobToOopClosure* f);
 180 
 181   static nmethod* scavenge_root_nmethods()            { return _scavenge_root_nmethods; }
 182   static void add_scavenge_root_nmethod(nmethod* nm);
 183   static void drop_scavenge_root_nmethod(nmethod* nm);
 184 
 185   // Printing/debugging
 186   static void print();                           // prints summary
 187   static void print_internals();
 188   static void print_memory_overhead();
 189   static void verify();                          // verifies the code cache
 190   static void print_trace(const char* event, CodeBlob* cb, int size = 0) PRODUCT_RETURN;
 191   static void print_summary(outputStream* st, bool detailed = true); // Prints a summary of the code cache usage
 192   static void log_state(outputStream* st);
 193   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"); }
 194   static void report_codemem_full(int code_blob_type, bool print);
 195 
 196   // Dcmd (Diagnostic commands)
 197   static void print_codelist(outputStream* st);
 198   static void print_layout(outputStream* st);
 199 
 200   // The full limits of the codeCache
 201   static address low_bound()                          { return _low_bound; }
 202   static address low_bound(int code_blob_type);
 203   static address high_bound()                         { return _high_bound; }
 204   static address high_bound(int code_blob_type);
 205 
 206   // Have to use far call instructions to call this pc.
 207   static bool is_far_target(address pc);
 208 
 209   // Profiling
 210   static size_t capacity();
 211   static size_t unallocated_capacity(int code_blob_type);
 212   static size_t unallocated_capacity();
 213   static size_t max_capacity();
 214 
 215   static double reverse_free_ratio(int code_blob_type);
 216 
 217   static bool needs_cache_clean()                     { return _needs_cache_clean; }
 218   static void set_needs_cache_clean(bool v)           { _needs_cache_clean = v;    }
 219   static void clear_inline_caches();                  // clear all inline caches
 220   static void cleanup_inline_caches();
 221 
 222   // Returns true if an own CodeHeap for the given CodeBlobType is available
 223   static bool heap_available(int code_blob_type);
 224 
 225   // Returns the CodeBlobType for the given CompiledMethod
 226   static int get_code_blob_type(CompiledMethod* cm) {
 227     return get_code_heap(cm)->code_blob_type();
 228   }
 229 
 230   static bool code_blob_type_accepts_compiled(int type) {
 231     bool result = type == CodeBlobType::All || type <= CodeBlobType::MethodProfiled;
 232     AOT_ONLY( result = result || type == CodeBlobType::AOT; )
 233     return result;
 234   }
 235 
 236   static bool code_blob_type_accepts_nmethod(int type) {
 237     return type == CodeBlobType::All || type <= CodeBlobType::MethodProfiled;
 238   }
 239 
 240   // Returns the CodeBlobType for the given compilation level
 241   static int get_code_blob_type(int comp_level) {
 242     if (comp_level == CompLevel_none ||
 243         comp_level == CompLevel_simple ||
 244         comp_level == CompLevel_full_optimization) {
 245       // Non profiled methods
 246       return CodeBlobType::MethodNonProfiled;
 247     } else if (comp_level == CompLevel_limited_profile ||
 248                comp_level == CompLevel_full_profile) {
 249       // Profiled methods
 250       return CodeBlobType::MethodProfiled;
 251     }
 252     ShouldNotReachHere();
 253     return 0;
 254   }
 255 
 256   static void verify_clean_inline_caches();
 257   static void verify_icholder_relocations();
 258 
 259   // Deoptimization
 260  private:
 261   static int  mark_for_deoptimization(KlassDepChange& changes);
 262 #ifdef HOTSWAP
 263   static int  mark_for_evol_deoptimization(InstanceKlass* dependee);
 264 #endif // HOTSWAP
 265 
 266  public:
 267   static void mark_all_nmethods_for_deoptimization();
 268   static int  mark_for_deoptimization(Method* dependee);
 269   static void make_marked_nmethods_not_entrant();
 270 
 271   // Flushing and deoptimization
 272   static void flush_dependents_on(InstanceKlass* dependee);
 273 #ifdef HOTSWAP
 274   // Flushing and deoptimization in case of evolution
 275   static void flush_evol_dependents_on(InstanceKlass* dependee);
 276 #endif // HOTSWAP
 277   // Support for fullspeed debugging
 278   static void flush_dependents_on_method(methodHandle dependee);
 279 
 280   // tells how many nmethods have dependencies
 281   static int number_of_nmethods_with_dependencies();
 282 
 283   static int get_codemem_full_count(int code_blob_type) {
 284     CodeHeap* heap = get_code_heap(code_blob_type);
 285     return (heap != NULL) ? heap->full_count() : 0;
 286   }
 287 };
 288 
 289 
 290 // Iterator to iterate over nmethods in the CodeCache.
 291 template <class T, class Filter> class CodeBlobIterator : public StackObj {
 292  private:
 293   CodeBlob* _code_blob;   // Current CodeBlob
 294   GrowableArrayIterator<CodeHeap*> _heap;
 295   GrowableArrayIterator<CodeHeap*> _end;
 296 
 297  public:
 298   CodeBlobIterator(T* nm = NULL) {
 299     if (Filter::heaps() == NULL) {
 300       return;
 301     }
 302     _heap = Filter::heaps()->begin();
 303     _end = Filter::heaps()->end();
 304     // If set to NULL, initialized by first call to next()
 305     _code_blob = (CodeBlob*)nm;
 306     if (nm != NULL) {
 307       while(!(*_heap)->contains_blob(_code_blob)) {
 308         ++_heap;
 309       }
 310       assert((*_heap)->contains_blob(_code_blob), "match not found");
 311     }
 312   }
 313 
 314   // Advance iterator to next blob
 315   bool next() {
 316     assert_locked_or_safepoint(CodeCache_lock);
 317 
 318     bool result = next_blob();
 319     while (!result && _heap != _end) {
 320       // Advance to next code heap of segmented code cache
 321       if (++_heap == _end) {
 322         break;
 323       }
 324       result = next_blob();
 325     }
 326 
 327     return result;
 328   }
 329 
 330   // Advance iterator to next alive blob
 331   bool next_alive() {
 332     bool result = next();
 333     while(result && !_code_blob->is_alive()) {
 334       result = next();
 335     }
 336     return result;
 337   }
 338 
 339   bool end()        const   { return _code_blob == NULL; }
 340   T* method() const   { return (T*)_code_blob; }
 341 
 342 private:
 343 
 344   // Advance iterator to the next blob in the current code heap
 345   bool next_blob() {
 346     if (_heap == _end) {
 347       return false;
 348     }
 349     CodeHeap *heap = *_heap;
 350     // Get first method CodeBlob
 351     if (_code_blob == NULL) {
 352       _code_blob = CodeCache::first_blob(heap);
 353       if (_code_blob == NULL) {
 354         return false;
 355       } else if (Filter::apply(_code_blob)) {
 356         return true;
 357       }
 358     }
 359     // Search for next method CodeBlob
 360     _code_blob = CodeCache::next_blob(heap, _code_blob);
 361     while (_code_blob != NULL && !Filter::apply(_code_blob)) {
 362       _code_blob = CodeCache::next_blob(heap, _code_blob);
 363     }
 364     return _code_blob != NULL;
 365   }
 366 };
 367 
 368 
 369 struct CompiledMethodFilter {
 370   static bool apply(CodeBlob* cb) { return cb->is_compiled(); }
 371   static const GrowableArray<CodeHeap*>* heaps() { return CodeCache::compiled_heaps(); }
 372 };
 373 
 374 
 375 struct NMethodFilter {
 376   static bool apply(CodeBlob* cb) { return cb->is_nmethod(); }
 377   static const GrowableArray<CodeHeap*>* heaps() { return CodeCache::nmethod_heaps(); }
 378 };
 379 
 380 
 381 typedef CodeBlobIterator<CompiledMethod, CompiledMethodFilter> CompiledMethodIterator;
 382 typedef CodeBlobIterator<nmethod, NMethodFilter> NMethodIterator;
 383 
 384 #endif // SHARE_VM_CODE_CODECACHE_HPP
--- EOF ---