1 /*
   2  * Copyright (c) 1997, 2012, 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 
  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   static nmethod* _saved_nmethods;          // Linked list of speculatively disconnected nmethods.
  61 
  62   static void verify_if_often() PRODUCT_RETURN;
  63 
  64   static void mark_scavenge_root_nmethods() PRODUCT_RETURN;
  65   static void verify_perm_nmethods(CodeBlobClosure* f_or_null) PRODUCT_RETURN;
  66 
  67  public:
  68 
  69   // Initialization
  70   static void initialize();
  71 
  72   // Allocation/administration
  73   static CodeBlob* allocate(int size, bool is_critical = false); // allocates a new CodeBlob
  74   static void commit(CodeBlob* cb);                 // called when the allocated CodeBlob has been filled
  75   static int alignment_unit();                      // guaranteed alignment of all CodeBlobs
  76   static int alignment_offset();                    // guaranteed offset of first CodeBlob byte within alignment unit (i.e., allocation header)
  77   static void free(CodeBlob* cb);                   // frees a CodeBlob
  78   static void flush();                              // flushes all CodeBlobs
  79   static bool contains(void *p);                    // returns whether p is included
  80   static void blobs_do(void f(CodeBlob* cb));       // iterates over all CodeBlobs
  81   static void blobs_do(CodeBlobClosure* f);         // iterates over all CodeBlobs
  82   static void nmethods_do(void f(nmethod* nm));     // iterates over all nmethods
  83   static void alive_nmethods_do(void f(nmethod* nm)); // iterates over all alive nmethods
  84 
  85   // Lookup
  86   static CodeBlob* find_blob(void* start);
  87   static nmethod*  find_nmethod(void* start);
  88 
  89   // Lookup that does not fail if you lookup a zombie method (if you call this, be sure to know
  90   // what you are doing)
  91   static CodeBlob* find_blob_unsafe(void* start) {
  92     // NMT can walk the stack before code cache is created
  93     if (_heap == NULL) return NULL;
  94 
  95     CodeBlob* result = (CodeBlob*)_heap->find_start(start);
  96     // this assert is too strong because the heap code will return the
  97     // heapblock containing start. That block can often be larger than
  98     // the codeBlob itself. If you look up an address that is within
  99     // the heapblock but not in the codeBlob you will assert.
 100     //
 101     // Most things will not lookup such bad addresses. However
 102     // AsyncGetCallTrace can see intermediate frames and get that kind
 103     // of invalid address and so can a developer using hsfind.
 104     //
 105     // The more correct answer is to return NULL if blob_contains() returns
 106     // false.
 107     // assert(result == NULL || result->blob_contains((address)start), "found wrong CodeBlob");
 108 
 109     if (result != NULL && !result->blob_contains((address)start)) {
 110       result = NULL;
 111     }
 112     return result;
 113   }
 114 
 115   // Iteration
 116   static CodeBlob* first();
 117   static CodeBlob* next (CodeBlob* cb);
 118   static CodeBlob* alive(CodeBlob *cb);
 119   static nmethod* alive_nmethod(CodeBlob *cb);
 120   static nmethod* first_nmethod();
 121   static nmethod* next_nmethod (CodeBlob* cb);
 122   static int       nof_blobs()                 { return _number_of_blobs; }
 123   static int       nof_adapters()              { return _number_of_adapters; }
 124   static int       nof_nmethods()              { return _number_of_nmethods; }
 125 
 126   // GC support
 127   static void gc_epilogue();
 128   static void gc_prologue();
 129   static void verify_oops();
 130   // If "unloading_occurred" is true, then unloads (i.e., breaks root links
 131   // to) any unmarked codeBlobs in the cache.  Sets "marked_for_unloading"
 132   // to "true" iff some code got unloaded.
 133   static void do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred);
 134   static void oops_do(OopClosure* f) {
 135     CodeBlobToOopClosure oopc(f, /*do_marking=*/ false);
 136     blobs_do(&oopc);
 137   }
 138   static void asserted_non_scavengable_nmethods_do(CodeBlobClosure* f = NULL) PRODUCT_RETURN;
 139   static void scavenge_root_nmethods_do(CodeBlobClosure* f);
 140 
 141   static nmethod* scavenge_root_nmethods()          { return _scavenge_root_nmethods; }
 142   static void set_scavenge_root_nmethods(nmethod* nm) { _scavenge_root_nmethods = nm; }
 143   static void add_scavenge_root_nmethod(nmethod* nm);
 144   static void drop_scavenge_root_nmethod(nmethod* nm);
 145   static void prune_scavenge_root_nmethods();
 146 
 147   // Printing/debugging
 148   static void print();                           // prints summary
 149   static void print_internals();
 150   static void verify();                          // verifies the code cache
 151   static void print_trace(const char* event, CodeBlob* cb, int size = 0) PRODUCT_RETURN;
 152   static void print_summary(outputStream* st, bool detailed = true); // Prints a summary of the code cache usage
 153   static void log_state(outputStream* st);
 154 
 155   // The full limits of the codeCache
 156   static address  low_bound()                    { return (address) _heap->low_boundary(); }
 157   static address  high_bound()                   { return (address) _heap->high_boundary(); }
 158 
 159   // Profiling
 160   static address first_address();                // first address used for CodeBlobs
 161   static address last_address();                 // last  address used for CodeBlobs
 162   static size_t  capacity()                      { return _heap->capacity(); }
 163   static size_t  max_capacity()                  { return _heap->max_capacity(); }
 164   static size_t  unallocated_capacity()          { return _heap->unallocated_capacity(); }
 165   static bool    needs_flushing()                { return unallocated_capacity() < CodeCacheFlushingMinimumFreeSpace; }
 166   static double  reverse_free_ratio();
 167 
 168   static bool needs_cache_clean()                { return _needs_cache_clean; }
 169   static void set_needs_cache_clean(bool v)      { _needs_cache_clean = v;    }
 170   static void clear_inline_caches();             // clear all inline caches
 171 
 172   static nmethod* reanimate_saved_code(Method* m);
 173   static void remove_saved_code(nmethod* nm);
 174   static void speculatively_disconnect(nmethod* nm);
 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 
 191 #endif // SHARE_VM_CODE_CODECACHE_HPP