src/share/vm/code/codeCache.cpp

Print this page




  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 #include "precompiled.hpp"
  26 #include "code/codeBlob.hpp"
  27 #include "code/codeCache.hpp"
  28 #include "code/compiledIC.hpp"
  29 #include "code/dependencies.hpp"
  30 #include "code/icBuffer.hpp"
  31 #include "code/nmethod.hpp"
  32 #include "code/pcDesc.hpp"
  33 #include "compiler/compileBroker.hpp"
  34 #include "gc_implementation/shared/markSweep.hpp"

  35 #include "memory/allocation.inline.hpp"
  36 #include "memory/gcLocker.hpp"
  37 #include "memory/iterator.hpp"
  38 #include "memory/resourceArea.hpp"
  39 #include "oops/method.hpp"
  40 #include "oops/objArrayOop.hpp"
  41 #include "oops/oop.inline.hpp"
  42 #include "runtime/handles.inline.hpp"
  43 #include "runtime/arguments.hpp"
  44 #include "runtime/icache.hpp"
  45 #include "runtime/java.hpp"
  46 #include "runtime/mutexLocker.hpp"
  47 #include "services/memoryService.hpp"
  48 #include "trace/tracing.hpp"
  49 #include "utilities/xmlstream.hpp"
  50 
  51 // Helper class for printing in CodeCache
  52 
  53 class CodeBlob_sizes {
  54  private:


  60   int relocation_size;
  61   int scopes_oop_size;
  62   int scopes_metadata_size;
  63   int scopes_data_size;
  64   int scopes_pcs_size;
  65 
  66  public:
  67   CodeBlob_sizes() {
  68     count            = 0;
  69     total_size       = 0;
  70     header_size      = 0;
  71     code_size        = 0;
  72     stub_size        = 0;
  73     relocation_size  = 0;
  74     scopes_oop_size  = 0;
  75     scopes_metadata_size  = 0;
  76     scopes_data_size = 0;
  77     scopes_pcs_size  = 0;
  78   }
  79 
  80   int total()                                    { return total_size; }

  81   bool is_empty()                                { return count == 0; }
  82 
  83   void print(const char* title) {
  84     tty->print_cr(" #%d %s = %dK (hdr %d%%,  loc %d%%, code %d%%, stub %d%%, [oops %d%%, data %d%%, pcs %d%%])",
  85                   count,



  86                   title,
  87                   total() / K,


  88                   header_size             * 100 / total_size,
  89                   relocation_size         * 100 / total_size,
  90                   code_size               * 100 / total_size,
  91                   stub_size               * 100 / total_size,
  92                   scopes_oop_size         * 100 / total_size,
  93                   scopes_metadata_size    * 100 / total_size,
  94                   scopes_data_size        * 100 / total_size,
  95                   scopes_pcs_size         * 100 / total_size);
  96   }

  97 
  98   void add(CodeBlob* cb) {
  99     count++;
 100     total_size       += cb->size();
 101     header_size      += cb->header_size();
 102     relocation_size  += cb->relocation_size();
 103     if (cb->is_nmethod()) {
 104       nmethod* nm = cb->as_nmethod_or_null();
 105       code_size        += nm->insts_size();
 106       stub_size        += nm->stub_size();
 107 
 108       scopes_oop_size  += nm->oops_size();
 109       scopes_metadata_size  += nm->metadata_size();
 110       scopes_data_size += nm->scopes_data_size();
 111       scopes_pcs_size  += nm->scopes_pcs_size();
 112     } else {
 113       code_size        += cb->code_size();
 114     }
 115   }
 116 };
 117 
 118 // CodeCache implementation
 119 
 120 CodeHeap * CodeCache::_heap = new CodeHeap();
 121 int CodeCache::_number_of_blobs = 0;
 122 int CodeCache::_number_of_adapters = 0;
 123 int CodeCache::_number_of_nmethods = 0;
 124 int CodeCache::_number_of_nmethods_with_dependencies = 0;
 125 bool CodeCache::_needs_cache_clean = false;
 126 nmethod* CodeCache::_scavenge_root_nmethods = NULL;
 127 nmethod* CodeCache::_saved_nmethods = NULL;
 128 
 129 int CodeCache::_codemem_full_count = 0;















 130 
 131 CodeBlob* CodeCache::first() {
 132   assert_locked_or_safepoint(CodeCache_lock);
 133   return (CodeBlob*)_heap->first();
 134 }
 135 
 136 
 137 CodeBlob* CodeCache::next(CodeBlob* cb) {
 138   assert_locked_or_safepoint(CodeCache_lock);
 139   return (CodeBlob*)_heap->next(cb);
 140 }
 141 
 142 
 143 CodeBlob* CodeCache::alive(CodeBlob *cb) {
 144   assert_locked_or_safepoint(CodeCache_lock);
 145   while (cb != NULL && !cb->is_alive()) cb = next(cb);
 146   return cb;
 147 }
 148 
 149 


 154 }
 155 
 156 nmethod* CodeCache::first_nmethod() {
 157   assert_locked_or_safepoint(CodeCache_lock);
 158   CodeBlob* cb = first();
 159   while (cb != NULL && !cb->is_nmethod()) {
 160     cb = next(cb);
 161   }
 162   return (nmethod*)cb;
 163 }
 164 
 165 nmethod* CodeCache::next_nmethod (CodeBlob* cb) {
 166   assert_locked_or_safepoint(CodeCache_lock);
 167   cb = next(cb);
 168   while (cb != NULL && !cb->is_nmethod()) {
 169     cb = next(cb);
 170   }
 171   return (nmethod*)cb;
 172 }
 173 
 174 static size_t maxCodeCacheUsed = 0;
 175 
 176 CodeBlob* CodeCache::allocate(int size, bool is_critical) {
 177   // Do not seize the CodeCache lock here--if the caller has not
 178   // already done so, we are going to lose bigtime, since the code
 179   // cache will contain a garbage CodeBlob until the caller can
 180   // run the constructor for the CodeBlob subclass he is busy
 181   // instantiating.
 182   guarantee(size >= 0, "allocation request must be reasonable");
 183   assert_locked_or_safepoint(CodeCache_lock);
 184   CodeBlob* cb = NULL;
 185   _number_of_blobs++;
 186   while (true) {
 187     cb = (CodeBlob*)_heap->allocate(size, is_critical);
 188     if (cb != NULL) break;
 189     if (!_heap->expand_by(CodeCacheExpansionSize)) {
 190       // Expansion failed
 191       return NULL;
 192     }
 193     if (PrintCodeCacheExtension) {
 194       ResourceMark rm;
 195       tty->print_cr("code cache extended to [" INTPTR_FORMAT ", " INTPTR_FORMAT "] (%d bytes)",
 196                     (intptr_t)_heap->low_boundary(), (intptr_t)_heap->high(),
 197                     (address)_heap->high() - (address)_heap->low_boundary());
 198     }
 199   }
 200   maxCodeCacheUsed = MAX2(maxCodeCacheUsed, ((address)_heap->high_boundary() -
 201                           (address)_heap->low_boundary()) - unallocated_capacity());
 202   verify_if_often();
 203   print_trace("allocation", cb, size);
 204   return cb;
 205 }
 206 
 207 void CodeCache::free(CodeBlob* cb) {
 208   assert_locked_or_safepoint(CodeCache_lock);
 209   verify_if_often();
 210 
 211   print_trace("free", cb);
 212   if (cb->is_nmethod()) {
 213     _number_of_nmethods--;
 214     if (((nmethod *)cb)->has_dependencies()) {
 215       _number_of_nmethods_with_dependencies--;
 216     }
 217   }
 218   if (cb->is_adapter_blob()) {
 219     _number_of_adapters--;
 220   }


 664 }
 665 
 666 
 667 void codeCache_init() {
 668   CodeCache::initialize();
 669 }
 670 
 671 //------------------------------------------------------------------------------------------------
 672 
 673 int CodeCache::number_of_nmethods_with_dependencies() {
 674   return _number_of_nmethods_with_dependencies;
 675 }
 676 
 677 void CodeCache::clear_inline_caches() {
 678   assert_locked_or_safepoint(CodeCache_lock);
 679   FOR_ALL_ALIVE_NMETHODS(nm) {
 680     nm->clear_inline_caches();
 681   }
 682 }
 683 
 684 #ifndef PRODUCT
 685 // used to keep track of how much time is spent in mark_for_deoptimization
 686 static elapsedTimer dependentCheckTime;
 687 static int dependentCheckCount = 0;
 688 #endif // PRODUCT
 689 
 690 
 691 int CodeCache::mark_for_deoptimization(DepChange& changes) {
 692   MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 693 
 694 #ifndef PRODUCT
 695   dependentCheckTime.start();
 696   dependentCheckCount++;
 697 #endif // PRODUCT
 698 
 699   int number_of_marked_CodeBlobs = 0;
 700 
 701   // search the hierarchy looking for nmethods which are affected by the loading of this class
 702 
 703   // then search the interfaces this class implements looking for nmethods
 704   // which might be dependent of the fact that an interface only had one
 705   // implementor.
 706 
 707   { No_Safepoint_Verifier nsv;
 708     for (DepChange::ContextStream str(changes, nsv); str.next(); ) {
 709       Klass* d = str.klass();


 829     p->verify();
 830   }
 831 }
 832 
 833 void CodeCache::report_codemem_full() {
 834   _codemem_full_count++;
 835   EventCodeCacheFull event;
 836   if (event.should_commit()) {
 837     event.set_startAddress((u8)low_bound());
 838     event.set_commitedTopAddress((u8)high());
 839     event.set_reservedTopAddress((u8)high_bound());
 840     event.set_entryCount(nof_blobs());
 841     event.set_methodCount(nof_nmethods());
 842     event.set_adaptorCount(nof_adapters());
 843     event.set_unallocatedCapacity(unallocated_capacity()/K);
 844     event.set_fullCount(_codemem_full_count);
 845     event.commit();
 846   }
 847 }
 848 


































































































 849 //------------------------------------------------------------------------------------------------
 850 // Non-product version
 851 
 852 #ifndef PRODUCT
 853 
 854 void CodeCache::verify_if_often() {
 855   if (VerifyCodeCacheOften) {
 856     _heap->verify();
 857   }
 858 }
 859 
 860 void CodeCache::print_trace(const char* event, CodeBlob* cb, int size) {
 861   if (PrintCodeCache2) {  // Need to add a new flag
 862     ResourceMark rm;
 863     if (size == 0)  size = cb->size();
 864     tty->print_cr("CodeCache %s:  addr: " INTPTR_FORMAT ", size: 0x%x", event, cb, size);


 865   }
 866 }
 867 
 868 void CodeCache::print_internals() {
 869   int nmethodCount = 0;
 870   int runtimeStubCount = 0;
 871   int adapterCount = 0;
 872   int deoptimizationStubCount = 0;
 873   int uncommonTrapStubCount = 0;
 874   int bufferBlobCount = 0;
 875   int total = 0;
 876   int nmethodAlive = 0;
 877   int nmethodNotEntrant = 0;
 878   int nmethodZombie = 0;
 879   int nmethodUnloaded = 0;
 880   int nmethodJava = 0;
 881   int nmethodNative = 0;
 882   int maxCodeSize = 0;
 883   ResourceMark rm;
 884 
 885   CodeBlob *cb;
 886   for (cb = first(); cb != NULL; cb = next(cb)) {
 887     total++;
















 888     if (cb->is_nmethod()) {
 889       nmethod* nm = (nmethod*)cb;
 890 
 891       if (Verbose && nm->method() != NULL) {
 892         ResourceMark rm;
 893         char *method_name = nm->method()->name_and_sig_as_C_string();
 894         tty->print("%s", method_name);
 895         if(nm->is_alive()) { tty->print_cr(" alive"); }
 896         if(nm->is_not_entrant()) { tty->print_cr(" not-entrant"); }
 897         if(nm->is_zombie()) { tty->print_cr(" zombie"); }
 898       }
 899 
 900       nmethodCount++;
 901 
 902       if(nm->is_alive()) { nmethodAlive++; }
 903       if(nm->is_not_entrant()) { nmethodNotEntrant++; }
 904       if(nm->is_zombie()) { nmethodZombie++; }
 905       if(nm->is_unloaded()) { nmethodUnloaded++; }
 906       if(nm->is_native_method()) { nmethodNative++; }
 907 
 908       if(nm->method() != NULL && nm->is_java_method()) {
 909         nmethodJava++;
 910         if (nm->insts_size() > maxCodeSize) {
 911           maxCodeSize = nm->insts_size();
 912         }
 913       }
 914     } else if (cb->is_runtime_stub()) {
 915       runtimeStubCount++;
 916     } else if (cb->is_deoptimization_stub()) {
 917       deoptimizationStubCount++;
 918     } else if (cb->is_uncommon_trap_stub()) {
 919       uncommonTrapStubCount++;
 920     } else if (cb->is_adapter_blob()) {
 921       adapterCount++;
 922     } else if (cb->is_buffer_blob()) {
 923       bufferBlobCount++;


 924     }
 925   }
 926 
 927   int bucketSize = 512;
 928   int bucketLimit = maxCodeSize / bucketSize + 1;
 929   int *buckets = NEW_C_HEAP_ARRAY(int, bucketLimit, mtCode);
 930   memset(buckets,0,sizeof(int) * bucketLimit);































 931 
 932   for (cb = first(); cb != NULL; cb = next(cb)) {
 933     if (cb->is_nmethod()) {
 934       nmethod* nm = (nmethod*)cb;











 935       if(nm->is_java_method()) {
 936         buckets[nm->insts_size() / bucketSize]++;
 937       }
 938     }
 939   }
 940   tty->print_cr("Code Cache Entries (total of %d)",total);
 941   tty->print_cr("-------------------------------------------------");
 942   tty->print_cr("nmethods: %d",nmethodCount);
 943   tty->print_cr("\talive: %d",nmethodAlive);
 944   tty->print_cr("\tnot_entrant: %d",nmethodNotEntrant);
 945   tty->print_cr("\tzombie: %d",nmethodZombie);
 946   tty->print_cr("\tunloaded: %d",nmethodUnloaded);
 947   tty->print_cr("\tjava: %d",nmethodJava);
 948   tty->print_cr("\tnative: %d",nmethodNative);
 949   tty->print_cr("runtime_stubs: %d",runtimeStubCount);
 950   tty->print_cr("adapters: %d",adapterCount);
 951   tty->print_cr("buffer blobs: %d",bufferBlobCount);
 952   tty->print_cr("deoptimization_stubs: %d",deoptimizationStubCount);
 953   tty->print_cr("uncommon_traps: %d",uncommonTrapStubCount);
 954   tty->print_cr("\nnmethod size distribution (non-zombie java)");
 955   tty->print_cr("-------------------------------------------------");
 956 
 957   for(int i=0; i<bucketLimit; i++) {
 958     if(buckets[i] != 0) {
 959       tty->print("%d - %d bytes",i*bucketSize,(i+1)*bucketSize);
 960       tty->fill_to(40);
 961       tty->print_cr("%d",buckets[i]);
 962     }
 963   }
 964 
 965   FREE_C_HEAP_ARRAY(int, buckets, mtCode);
 966 }
 967 
 968 #endif // !PRODUCT
 969 
 970 void CodeCache::print() {
 971   print_summary(tty);
 972 
 973 #ifndef PRODUCT
 974   if (!Verbose) return;
 975 
 976   CodeBlob_sizes live;
 977   CodeBlob_sizes dead;
 978 
 979   FOR_ALL_BLOBS(p) {
 980     if (!p->is_alive()) {
 981       dead.add(p);
 982     } else {
 983       live.add(p);
 984     }
 985   }
 986 
 987   tty->print_cr("CodeCache:");
 988 
 989   tty->print_cr("nmethod dependency checking time %f", dependentCheckTime.seconds(),
 990                 dependentCheckTime.seconds() / dependentCheckCount);
 991 
 992   if (!live.is_empty()) {
 993     live.print("live");
 994   }
 995   if (!dead.is_empty()) {
 996     dead.print("dead");
 997   }
 998 
 999 
1000   if (WizardMode) {
1001      // print the oop_map usage
1002     int code_size = 0;
1003     int number_of_blobs = 0;
1004     int number_of_oop_maps = 0;
1005     int map_size = 0;
1006     FOR_ALL_BLOBS(p) {
1007       if (p->is_alive()) {
1008         number_of_blobs++;
1009         code_size += p->code_size();
1010         OopMapSet* set = p->oop_maps();
1011         if (set != NULL) {
1012           number_of_oop_maps += set->size();
1013           map_size           += set->heap_size();
1014         }
1015       }
1016     }
1017     tty->print_cr("OopMaps");
1018     tty->print_cr("  #blobs    = %d", number_of_blobs);
1019     tty->print_cr("  code size = %d", code_size);
1020     tty->print_cr("  #oop_maps = %d", number_of_oop_maps);
1021     tty->print_cr("  map size  = %d", map_size);
1022   }
1023 
1024 #endif // !PRODUCT
1025 }
1026 
1027 void CodeCache::print_summary(outputStream* st, bool detailed) {
1028   size_t total = (_heap->high_boundary() - _heap->low_boundary());
1029   st->print_cr("CodeCache: size=" SIZE_FORMAT "Kb used=" SIZE_FORMAT
1030                "Kb max_used=" SIZE_FORMAT "Kb free=" SIZE_FORMAT "Kb",
1031                total/K, (total - unallocated_capacity())/K,
1032                maxCodeCacheUsed/K, unallocated_capacity()/K);
1033 
1034   if (detailed) {
1035     st->print_cr(" bounds [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT "]",
1036                  _heap->low_boundary(),
1037                  _heap->high(),
1038                  _heap->high_boundary());
1039     st->print_cr(" total_blobs=" UINT32_FORMAT " nmethods=" UINT32_FORMAT
1040                  " adapters=" UINT32_FORMAT,
1041                  nof_blobs(), nof_nmethods(), nof_adapters());
1042     st->print_cr(" compilation: %s", CompileBroker::should_compile_new_jobs() ?
1043                  "enabled" : Arguments::mode() == Arguments::_int ?
1044                  "disabled (interpreter mode)" :
1045                  "disabled (not enough contiguous free space left)");
1046   }
1047 }
1048 
1049 void CodeCache::log_state(outputStream* st) {
1050   st->print(" total_blobs='" UINT32_FORMAT "' nmethods='" UINT32_FORMAT "'"
1051             " adapters='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'",
1052             nof_blobs(), nof_nmethods(), nof_adapters(),
1053             unallocated_capacity());
1054 }
1055 


  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 #include "precompiled.hpp"
  26 #include "code/codeBlob.hpp"
  27 #include "code/codeCache.hpp"
  28 #include "code/compiledIC.hpp"
  29 #include "code/dependencies.hpp"
  30 #include "code/icBuffer.hpp"
  31 #include "code/nmethod.hpp"
  32 #include "code/pcDesc.hpp"
  33 #include "compiler/compileBroker.hpp"
  34 #include "gc_implementation/shared/markSweep.hpp"
  35 #include "interpreter/abstractInterpreter.hpp"
  36 #include "memory/allocation.inline.hpp"
  37 #include "memory/gcLocker.hpp"
  38 #include "memory/iterator.hpp"
  39 #include "memory/resourceArea.hpp"
  40 #include "oops/method.hpp"
  41 #include "oops/objArrayOop.hpp"
  42 #include "oops/oop.inline.hpp"
  43 #include "runtime/handles.inline.hpp"
  44 #include "runtime/arguments.hpp"
  45 #include "runtime/icache.hpp"
  46 #include "runtime/java.hpp"
  47 #include "runtime/mutexLocker.hpp"
  48 #include "services/memoryService.hpp"
  49 #include "trace/tracing.hpp"
  50 #include "utilities/xmlstream.hpp"
  51 
  52 // Helper class for printing in CodeCache
  53 
  54 class CodeBlob_sizes {
  55  private:


  61   int relocation_size;
  62   int scopes_oop_size;
  63   int scopes_metadata_size;
  64   int scopes_data_size;
  65   int scopes_pcs_size;
  66 
  67  public:
  68   CodeBlob_sizes() {
  69     count                = 0;
  70     total_size           = 0;
  71     header_size          = 0;
  72     code_size            = 0;
  73     stub_size            = 0;
  74     relocation_size      = 0;
  75     scopes_oop_size      = 0;
  76     scopes_metadata_size = 0;
  77     scopes_data_size     = 0;
  78     scopes_pcs_size      = 0;
  79   }
  80 
  81   int get_total_size()  { return total_size; }
  82   int get_total_count() { return count; }
  83   bool is_empty()       { return count == 0; }
  84 
  85   void print(const char* title) {
  86     const int len = strnlen(title, 25);
  87     if (count == 0) {
  88       tty->print_cr("%s%*d#", title, 25-len, count);
  89     } else {
  90       tty->print_cr("%s%*d#%7d kB\t(hdr %2u%%, loc %2u%%, code %2u%%, stub %2u%%, [oops %2u%%, data %2u%%, pcs %2u%%])",
  91                     title,
  92                     25-len,
  93                     count,
  94                     total_size / K,
  95                     header_size             * 100 / total_size,
  96                     relocation_size         * 100 / total_size,
  97                     code_size               * 100 / total_size,
  98                     stub_size               * 100 / total_size,
  99                     scopes_oop_size         * 100 / total_size,
 100                     scopes_metadata_size    * 100 / total_size,
 101                     scopes_data_size        * 100 / total_size,
 102                     scopes_pcs_size         * 100 / total_size);
 103         }
 104   }
 105 
 106   void add(CodeBlob* cb) {
 107     count++;
 108     total_size       += cb->size();
 109     header_size      += cb->header_size();
 110     relocation_size  += cb->relocation_size();
 111     if (cb->is_nmethod()) {
 112       nmethod* nm = cb->as_nmethod_or_null();
 113       code_size        += nm->insts_size();
 114       stub_size        += nm->stub_size();
 115 
 116       scopes_oop_size  += nm->oops_size();
 117       scopes_metadata_size  += nm->metadata_size();
 118       scopes_data_size += nm->scopes_data_size();
 119       scopes_pcs_size  += nm->scopes_pcs_size();
 120     } else {
 121       code_size        += cb->code_size();
 122     }
 123   }
 124 };
 125 
 126 // CodeCache implementation
 127 
 128 CodeHeap* CodeCache::_heap = new CodeHeap();
 129 int CodeCache::_number_of_blobs = 0;
 130 int CodeCache::_number_of_adapters = 0;
 131 int CodeCache::_number_of_nmethods = 0;
 132 int CodeCache::_number_of_nmethods_with_dependencies = 0;
 133 bool CodeCache::_needs_cache_clean = false;
 134 nmethod* CodeCache::_scavenge_root_nmethods = NULL;
 135 nmethod* CodeCache::_saved_nmethods = NULL;
 136 
 137 int CodeCache::_codemem_full_count = 0;
 138 static size_t _max_code_cache_used = 0;
 139 
 140 // Code cache printing options
 141 static bool _print_details        = false;
 142 static bool _print_trace          = false;
 143 static bool _print_content        = false;
 144 static bool _print_oop_map_usage  = false;
 145 static bool _print_dep_check_time = false;
 146 
 147 #ifndef PRODUCT
 148 // used to keep track of how much time is spent in mark_for_deoptimization
 149 static elapsedTimer dependentCheckTime;
 150 static int dependentCheckCount = 0;
 151 #endif // PRODUCT
 152 
 153 
 154 CodeBlob* CodeCache::first() {
 155   assert_locked_or_safepoint(CodeCache_lock);
 156   return (CodeBlob*)_heap->first();
 157 }
 158 
 159 
 160 CodeBlob* CodeCache::next(CodeBlob* cb) {
 161   assert_locked_or_safepoint(CodeCache_lock);
 162   return (CodeBlob*)_heap->next(cb);
 163 }
 164 
 165 
 166 CodeBlob* CodeCache::alive(CodeBlob *cb) {
 167   assert_locked_or_safepoint(CodeCache_lock);
 168   while (cb != NULL && !cb->is_alive()) cb = next(cb);
 169   return cb;
 170 }
 171 
 172 


 177 }
 178 
 179 nmethod* CodeCache::first_nmethod() {
 180   assert_locked_or_safepoint(CodeCache_lock);
 181   CodeBlob* cb = first();
 182   while (cb != NULL && !cb->is_nmethod()) {
 183     cb = next(cb);
 184   }
 185   return (nmethod*)cb;
 186 }
 187 
 188 nmethod* CodeCache::next_nmethod (CodeBlob* cb) {
 189   assert_locked_or_safepoint(CodeCache_lock);
 190   cb = next(cb);
 191   while (cb != NULL && !cb->is_nmethod()) {
 192     cb = next(cb);
 193   }
 194   return (nmethod*)cb;
 195 }
 196 


 197 CodeBlob* CodeCache::allocate(int size, bool is_critical) {
 198   // Do not seize the CodeCache lock here--if the caller has not
 199   // already done so, we are going to lose bigtime, since the code
 200   // cache will contain a garbage CodeBlob until the caller can
 201   // run the constructor for the CodeBlob subclass he is busy
 202   // instantiating.
 203   guarantee(size >= 0, "allocation request must be reasonable");
 204   assert_locked_or_safepoint(CodeCache_lock);
 205   CodeBlob* cb = NULL;
 206   _number_of_blobs++;
 207   while (true) {
 208     cb = (CodeBlob*)_heap->allocate(size, is_critical);
 209     if (cb != NULL) break;
 210     if (!_heap->expand_by(CodeCacheExpansionSize)) {
 211       // Expansion failed
 212       return NULL;
 213     }
 214     if (PrintCodeCacheExtension) {
 215       ResourceMark rm;
 216       tty->print_cr("code cache extended to [" INTPTR_FORMAT ", " INTPTR_FORMAT "] (%d bytes)",
 217                     (intptr_t)_heap->low_boundary(), (intptr_t)_heap->high(),
 218                     (address)_heap->high() - (address)_heap->low_boundary());
 219     }
 220   }
 221   _max_code_cache_used = MAX2(_max_code_cache_used, ((address)_heap->high_boundary() -
 222                           (address)_heap->low_boundary()) - unallocated_capacity());
 223   verify_if_often();
 224   print_trace("allocation", cb, size);
 225   return cb;
 226 }
 227 
 228 void CodeCache::free(CodeBlob* cb) {
 229   assert_locked_or_safepoint(CodeCache_lock);
 230   verify_if_often();
 231 
 232   print_trace("free", cb);
 233   if (cb->is_nmethod()) {
 234     _number_of_nmethods--;
 235     if (((nmethod *)cb)->has_dependencies()) {
 236       _number_of_nmethods_with_dependencies--;
 237     }
 238   }
 239   if (cb->is_adapter_blob()) {
 240     _number_of_adapters--;
 241   }


 685 }
 686 
 687 
 688 void codeCache_init() {
 689   CodeCache::initialize();
 690 }
 691 
 692 //------------------------------------------------------------------------------------------------
 693 
 694 int CodeCache::number_of_nmethods_with_dependencies() {
 695   return _number_of_nmethods_with_dependencies;
 696 }
 697 
 698 void CodeCache::clear_inline_caches() {
 699   assert_locked_or_safepoint(CodeCache_lock);
 700   FOR_ALL_ALIVE_NMETHODS(nm) {
 701     nm->clear_inline_caches();
 702   }
 703 }
 704 






 705 
 706 int CodeCache::mark_for_deoptimization(DepChange& changes) {
 707   MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
 708 
 709 #ifndef PRODUCT
 710   dependentCheckTime.start();
 711   dependentCheckCount++;
 712 #endif // PRODUCT
 713 
 714   int number_of_marked_CodeBlobs = 0;
 715 
 716   // search the hierarchy looking for nmethods which are affected by the loading of this class
 717 
 718   // then search the interfaces this class implements looking for nmethods
 719   // which might be dependent of the fact that an interface only had one
 720   // implementor.
 721 
 722   { No_Safepoint_Verifier nsv;
 723     for (DepChange::ContextStream str(changes, nsv); str.next(); ) {
 724       Klass* d = str.klass();


 844     p->verify();
 845   }
 846 }
 847 
 848 void CodeCache::report_codemem_full() {
 849   _codemem_full_count++;
 850   EventCodeCacheFull event;
 851   if (event.should_commit()) {
 852     event.set_startAddress((u8)low_bound());
 853     event.set_commitedTopAddress((u8)high());
 854     event.set_reservedTopAddress((u8)high_bound());
 855     event.set_entryCount(nof_blobs());
 856     event.set_methodCount(nof_nmethods());
 857     event.set_adaptorCount(nof_adapters());
 858     event.set_unallocatedCapacity(unallocated_capacity()/K);
 859     event.set_fullCount(_codemem_full_count);
 860     event.commit();
 861   }
 862 }
 863 
 864 void CodeCache::init_printing_options(const char* str) {
 865   if (strcmp("on", str) == 0) {
 866     _print_details = true;
 867   } else if (strcmp("trace", str) == 0) {
 868     _print_trace = true;
 869   } else if (strcmp("content", str) == 0) {
 870     _print_content = true;
 871   } else if(strcmp("oop_map", str) == 0) {
 872     _print_oop_map_usage = true;
 873   } else if(strcmp("dep_check_time", str) == 0) {
 874     _print_dep_check_time = true;
 875   } else if (strcmp("all", str) == 0) {
 876     _print_details = true;
 877     _print_trace = true;
 878     _print_content = true;
 879     _print_oop_map_usage = true;
 880     _print_dep_check_time = true;
 881   } else if (strcmp("off", str) != 0) {
 882     vm_exit_during_initialization("Syntax error, expecting -XX:PrintCodeCacheDetails=[off|on|all|trace|content|oop_map|dep_check_time]", NULL);
 883   }
 884 }
 885 
 886 void CodeCache::print() {
 887   print_summary(tty);
 888 
 889 #ifndef PRODUCT
 890   if (_print_details) {
 891     print_details();
 892   }
 893   if (_print_content) {
 894     print_content();
 895   }
 896   if (WizardMode || _print_oop_map_usage) {
 897     print_oop_map_usage();
 898   }
 899   if (_print_dep_check_time) {
 900     print_dependency_checking_time();
 901   }
 902 #endif
 903 }
 904 
 905 void CodeCache::print_summary(outputStream* st, bool detailed) {
 906   size_t total = (_heap->high_boundary() - _heap->low_boundary());
 907   st->print_cr("CodeCache: size=" SIZE_FORMAT "kB used=" SIZE_FORMAT
 908                "kB max_used=" SIZE_FORMAT "kB free=" SIZE_FORMAT "kB",
 909                total/K, (total - unallocated_capacity())/K,
 910                _max_code_cache_used/K, unallocated_capacity()/K);
 911 
 912 
 913   if (detailed) {
 914     int interpreter_size = AbstractInterpreter::code()->total_space() / K;
 915     CodeBlob_sizes live_nm;
 916     CodeBlob_sizes dead_nm;
 917     CodeBlob_sizes stubs;
 918     CodeBlob_sizes adapters;
 919 
 920     FOR_ALL_BLOBS(p) {
 921       // live or not-entrant methods
 922       if (p->is_nmethod()) {
 923         if (p->is_alive()) {
 924           live_nm.add(p);
 925         } else {
 926           dead_nm.add(p);
 927         }
 928       } else {
 929         if (p->is_adapter_blob()) {
 930           adapters.add(p);
 931         } else {
 932           stubs.add(p);
 933         }
 934       }
 935     }
 936     st->print_cr(" Interpreter=%dkB live_nmethods=%u(%ukB) dead_nmethods=%u(%ukB) stubs=%u(%ukB) adapters=%u(%ukB)",
 937                        interpreter_size, live_nm.get_total_count(), live_nm.get_total_size() / K,
 938                        dead_nm.get_total_count(), dead_nm.get_total_size() / K,
 939                        stubs.get_total_count(), stubs.get_total_size() / K,
 940                        adapters.get_total_count(), adapters.get_total_size() / K);
 941 
 942     st->print_cr(" bounds [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT "]",
 943                  _heap->low_boundary(),
 944                  _heap->high(),
 945                  _heap->high_boundary());
 946 
 947     st->print_cr(" compilation: %s", CompileBroker::should_compile_new_jobs() ?
 948                  "enabled" : Arguments::mode() == Arguments::_int ?
 949                  "disabled (interpreter mode)" :
 950                  "disabled (not enough contiguous free space left)");
 951   }
 952 }
 953 
 954 void CodeCache::log_state(outputStream* st) {
 955   st->print(" total_blobs='" UINT32_FORMAT "' nmethods='" UINT32_FORMAT "'"
 956             " adapters='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'",
 957             nof_blobs(), nof_nmethods(), nof_adapters(),
 958             unallocated_capacity());
 959 }
 960 
 961 
 962 //------------------------------------------------------------------------------------------------
 963 // Non-product version

 964 #ifndef PRODUCT
 965 
 966 void CodeCache::verify_if_often() {
 967   if (VerifyCodeCacheOften) {
 968     _heap->verify();
 969   }
 970 }
 971 
 972 void CodeCache::print_trace(const char* event, CodeBlob* cb, int size) {
 973   if (_print_trace) {
 974     ResourceMark rm;
 975     if (size == 0)  {
 976       size = cb->size();
 977     }
 978     tty->print_cr("CodeCache %s:  addr: " INTPTR_FORMAT ", size: %dB", event, cb, size);
 979   }
 980 }
 981 
 982 void CodeCache::print_details() {














 983   ResourceMark rm;
 984 
 985   CodeBlob_sizes runtime_stubs;
 986   CodeBlob_sizes adapters;
 987   CodeBlob_sizes deoptimization_stubs;
 988   CodeBlob_sizes uncommon_trap_stubs;
 989   CodeBlob_sizes buffer_blobs;
 990   CodeBlob_sizes in_use;
 991   CodeBlob_sizes not_entrant;
 992   CodeBlob_sizes zombie;
 993   CodeBlob_sizes unloaded;
 994   CodeBlob_sizes java_methods;
 995   CodeBlob_sizes native_methods;
 996   CodeBlob_sizes other_entries;
 997   CodeBlob_sizes tiers[CompLevel_full_optimization + 1];
 998 
 999   int total_entries = 0;
1000   int max_code_size = 0;
1001 
1002   FOR_ALL_BLOBS(cb) {
1003     total_entries++;
1004     if (cb->is_nmethod()) {
1005       nmethod* nm = (nmethod*)cb;
1006 
1007       if (nm->is_in_use()) {
1008         in_use.add(nm);
1009       } else if (nm->is_not_entrant()) {
1010         not_entrant.add(nm);
1011       } else if (nm->is_zombie()) {
1012         zombie.add(nm);
1013       } else if (nm->is_unloaded()) {
1014         unloaded.add(nm);
1015       }
1016 
1017       if(nm->is_native_method()) {
1018         native_methods.add(nm);
1019       }
1020 
1021       // Native methods are Tier 0
1022       tiers[nm->comp_level()].add(nm);
1023 
1024       if (nm->method() != NULL && nm->is_java_method()) {
1025         java_methods.add(nm);
1026         if (nm->insts_size() > max_code_size) {
1027           max_code_size = nm->insts_size();
1028         }
1029       }
1030     } else if (cb->is_runtime_stub()) {
1031       runtime_stubs.add(cb);
1032     } else if (cb->is_deoptimization_stub()) {
1033       deoptimization_stubs.add(cb);
1034     } else if (cb->is_uncommon_trap_stub()) {
1035       uncommon_trap_stubs.add(cb);
1036     } else if (cb->is_adapter_blob()) {
1037       adapters.add(cb);
1038     } else if (cb->is_buffer_blob()) {
1039       buffer_blobs.add(cb);
1040     } else {
1041       other_entries.add(cb);
1042     }
1043   }
1044 
1045   tty->print_cr("\nCode cache entries: (total of #%d)", total_entries);
1046   int total_nm_count = tiers[0].get_total_count() + tiers[1].get_total_count() + tiers[2].get_total_count() +
1047                                tiers[3].get_total_count() + tiers[4].get_total_count();
1048   int total_nm_size =  tiers[0].get_total_size() + tiers[1].get_total_size() + tiers[2].get_total_size() +
1049                                tiers[3].get_total_size() + tiers[4].get_total_size();
1050   tty->print_cr("nmethods:\t%6d# 7%d kB",  total_nm_count, total_nm_size / K);
1051   java_methods.print(" Java");
1052   tiers[1].print("  Tier 1");
1053   tiers[2].print("  Tier 2");
1054   tiers[3].print("  Tier 3");
1055   tiers[4].print("  Tier 4");
1056   native_methods.print(" Native");
1057 
1058   runtime_stubs.print("runtime stubs");
1059   adapters.print("adapters");
1060   buffer_blobs.print("buffer blobs");
1061   deoptimization_stubs.print("deoptimization stubs");
1062   uncommon_trap_stubs.print("uncommon trap stubs");
1063   other_entries.print("others");
1064 
1065   tty->print_cr("\nnmethod state distribution");
1066   in_use.print(" in-use");
1067   not_entrant.print(" not-entrant");
1068   zombie.print(" zombie");
1069   unloaded.print(" unloaded");
1070 }
1071 
1072 void CodeCache::print_content() {
1073   const int bucketSize = 512;
1074   const int bucketLimit = _max_code_cache_used / bucketSize + 1;
1075   int* buckets = NEW_C_HEAP_ARRAY_RETURN_NULL(int, bucketLimit, mtInternal);
1076   if (buckets == NULL) {
1077     return;
1078   }
1079   memset(buckets, 0, sizeof(int) * bucketLimit);
1080 
1081   FOR_ALL_BLOBS(cb) {
1082     if (cb->is_nmethod()) {
1083       nmethod* nm = (nmethod*)cb;
1084       if (nm->method() != NULL) {
1085         ResourceMark rm;
1086         const char *method_name = nm->method()->name_and_sig_as_C_string();
1087         if (nm->is_in_use()) {
1088           tty->print("in-use:      ");
1089             } else if (nm->is_not_entrant()) {
1090           tty->print("not-entrant: ");
1091         } else if (nm->is_zombie()) {
1092           tty->print("zombie:      ");
1093             }
1094             tty->print_cr("%s", method_name);
1095             if(nm->is_java_method()) {
1096               buckets[nm->insts_size() / bucketSize]++;
1097             }
1098           }
1099     }
1100   }
1101 












1102   tty->print_cr("\nnmethod size distribution (non-zombie java)");

1103 
1104   for (int i = 0; i < bucketLimit; i++) {
1105     if (buckets[i] != 0) {
1106       tty->print("%d - %d bytes",i*bucketSize,(i+1)*bucketSize);
1107       tty->fill_to(40);
1108       tty->print_cr("%d", buckets[i]);
1109     }
1110   }
1111   FREE_C_HEAP_ARRAY(int, buckets, mtInternal);

1112 }
1113 
1114 void CodeCache::print_oop_map_usage() {

































1115   int code_size = 0;
1116   int number_of_blobs = 0;
1117   int number_of_oop_maps = 0;
1118   int map_size = 0;
1119   FOR_ALL_BLOBS(p) {
1120     if (p->is_alive()) {
1121       number_of_blobs++;
1122       code_size += p->code_size();
1123       OopMapSet* set = p->oop_maps();
1124       if (set != NULL) {
1125         number_of_oop_maps += set->size();
1126         map_size           += set->heap_size();
1127       }
1128     }
1129   }
1130   tty->print_cr("OopMaps");
1131   tty->print_cr("  #blobs    = %d", number_of_blobs);
1132   tty->print_cr("  code size = %d", code_size);
1133   tty->print_cr("  #oop_maps = %d", number_of_oop_maps);
1134   tty->print_cr("  map size  = %d", map_size);

























1135 }
1136 
1137 void CodeCache::print_dependency_checking_time() {
1138   tty->print_cr("total nmethod dependency checking time: %f[s]", dependentCheckTime.seconds());
1139   tty->print_cr("total number of dependency checks     : %d", dependentCheckCount);


1140 }
1141 #endif // !PRODUCT