src/share/vm/code/codeCache.cpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/code/codeCache.cpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,969 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "code/codeBlob.hpp"
    1.30 +#include "code/codeCache.hpp"
    1.31 +#include "code/compiledIC.hpp"
    1.32 +#include "code/dependencies.hpp"
    1.33 +#include "code/icBuffer.hpp"
    1.34 +#include "code/nmethod.hpp"
    1.35 +#include "code/pcDesc.hpp"
    1.36 +#include "compiler/compileBroker.hpp"
    1.37 +#include "gc_implementation/shared/markSweep.hpp"
    1.38 +#include "memory/allocation.inline.hpp"
    1.39 +#include "memory/gcLocker.hpp"
    1.40 +#include "memory/iterator.hpp"
    1.41 +#include "memory/resourceArea.hpp"
    1.42 +#include "oops/method.hpp"
    1.43 +#include "oops/objArrayOop.hpp"
    1.44 +#include "oops/oop.inline.hpp"
    1.45 +#include "runtime/handles.inline.hpp"
    1.46 +#include "runtime/arguments.hpp"
    1.47 +#include "runtime/icache.hpp"
    1.48 +#include "runtime/java.hpp"
    1.49 +#include "runtime/mutexLocker.hpp"
    1.50 +#include "services/memoryService.hpp"
    1.51 +#include "trace/tracing.hpp"
    1.52 +#include "utilities/xmlstream.hpp"
    1.53 +
    1.54 +// Helper class for printing in CodeCache
    1.55 +
    1.56 +class CodeBlob_sizes {
    1.57 + private:
    1.58 +  int count;
    1.59 +  int total_size;
    1.60 +  int header_size;
    1.61 +  int code_size;
    1.62 +  int stub_size;
    1.63 +  int relocation_size;
    1.64 +  int scopes_oop_size;
    1.65 +  int scopes_metadata_size;
    1.66 +  int scopes_data_size;
    1.67 +  int scopes_pcs_size;
    1.68 +
    1.69 + public:
    1.70 +  CodeBlob_sizes() {
    1.71 +    count            = 0;
    1.72 +    total_size       = 0;
    1.73 +    header_size      = 0;
    1.74 +    code_size        = 0;
    1.75 +    stub_size        = 0;
    1.76 +    relocation_size  = 0;
    1.77 +    scopes_oop_size  = 0;
    1.78 +    scopes_metadata_size  = 0;
    1.79 +    scopes_data_size = 0;
    1.80 +    scopes_pcs_size  = 0;
    1.81 +  }
    1.82 +
    1.83 +  int total()                                    { return total_size; }
    1.84 +  bool is_empty()                                { return count == 0; }
    1.85 +
    1.86 +  void print(const char* title) {
    1.87 +    tty->print_cr(" #%d %s = %dK (hdr %d%%,  loc %d%%, code %d%%, stub %d%%, [oops %d%%, metadata %d%%, data %d%%, pcs %d%%])",
    1.88 +                  count,
    1.89 +                  title,
    1.90 +                  (int)(total() / K),
    1.91 +                  header_size             * 100 / total_size,
    1.92 +                  relocation_size         * 100 / total_size,
    1.93 +                  code_size               * 100 / total_size,
    1.94 +                  stub_size               * 100 / total_size,
    1.95 +                  scopes_oop_size         * 100 / total_size,
    1.96 +                  scopes_metadata_size    * 100 / total_size,
    1.97 +                  scopes_data_size        * 100 / total_size,
    1.98 +                  scopes_pcs_size         * 100 / total_size);
    1.99 +  }
   1.100 +
   1.101 +  void add(CodeBlob* cb) {
   1.102 +    count++;
   1.103 +    total_size       += cb->size();
   1.104 +    header_size      += cb->header_size();
   1.105 +    relocation_size  += cb->relocation_size();
   1.106 +    if (cb->is_nmethod()) {
   1.107 +      nmethod* nm = cb->as_nmethod_or_null();
   1.108 +      code_size        += nm->insts_size();
   1.109 +      stub_size        += nm->stub_size();
   1.110 +
   1.111 +      scopes_oop_size  += nm->oops_size();
   1.112 +      scopes_metadata_size  += nm->metadata_size();
   1.113 +      scopes_data_size += nm->scopes_data_size();
   1.114 +      scopes_pcs_size  += nm->scopes_pcs_size();
   1.115 +    } else {
   1.116 +      code_size        += cb->code_size();
   1.117 +    }
   1.118 +  }
   1.119 +};
   1.120 +
   1.121 +// CodeCache implementation
   1.122 +
   1.123 +CodeHeap * CodeCache::_heap = new CodeHeap();
   1.124 +int CodeCache::_number_of_blobs = 0;
   1.125 +int CodeCache::_number_of_adapters = 0;
   1.126 +int CodeCache::_number_of_nmethods = 0;
   1.127 +int CodeCache::_number_of_nmethods_with_dependencies = 0;
   1.128 +bool CodeCache::_needs_cache_clean = false;
   1.129 +nmethod* CodeCache::_scavenge_root_nmethods = NULL;
   1.130 +
   1.131 +int CodeCache::_codemem_full_count = 0;
   1.132 +
   1.133 +CodeBlob* CodeCache::first() {
   1.134 +  assert_locked_or_safepoint(CodeCache_lock);
   1.135 +  return (CodeBlob*)_heap->first();
   1.136 +}
   1.137 +
   1.138 +
   1.139 +CodeBlob* CodeCache::next(CodeBlob* cb) {
   1.140 +  assert_locked_or_safepoint(CodeCache_lock);
   1.141 +  return (CodeBlob*)_heap->next(cb);
   1.142 +}
   1.143 +
   1.144 +
   1.145 +CodeBlob* CodeCache::alive(CodeBlob *cb) {
   1.146 +  assert_locked_or_safepoint(CodeCache_lock);
   1.147 +  while (cb != NULL && !cb->is_alive()) cb = next(cb);
   1.148 +  return cb;
   1.149 +}
   1.150 +
   1.151 +
   1.152 +nmethod* CodeCache::alive_nmethod(CodeBlob* cb) {
   1.153 +  assert_locked_or_safepoint(CodeCache_lock);
   1.154 +  while (cb != NULL && (!cb->is_alive() || !cb->is_nmethod())) cb = next(cb);
   1.155 +  return (nmethod*)cb;
   1.156 +}
   1.157 +
   1.158 +nmethod* CodeCache::first_nmethod() {
   1.159 +  assert_locked_or_safepoint(CodeCache_lock);
   1.160 +  CodeBlob* cb = first();
   1.161 +  while (cb != NULL && !cb->is_nmethod()) {
   1.162 +    cb = next(cb);
   1.163 +  }
   1.164 +  return (nmethod*)cb;
   1.165 +}
   1.166 +
   1.167 +nmethod* CodeCache::next_nmethod (CodeBlob* cb) {
   1.168 +  assert_locked_or_safepoint(CodeCache_lock);
   1.169 +  cb = next(cb);
   1.170 +  while (cb != NULL && !cb->is_nmethod()) {
   1.171 +    cb = next(cb);
   1.172 +  }
   1.173 +  return (nmethod*)cb;
   1.174 +}
   1.175 +
   1.176 +static size_t maxCodeCacheUsed = 0;
   1.177 +
   1.178 +CodeBlob* CodeCache::allocate(int size, bool is_critical) {
   1.179 +  // Do not seize the CodeCache lock here--if the caller has not
   1.180 +  // already done so, we are going to lose bigtime, since the code
   1.181 +  // cache will contain a garbage CodeBlob until the caller can
   1.182 +  // run the constructor for the CodeBlob subclass he is busy
   1.183 +  // instantiating.
   1.184 +  guarantee(size >= 0, "allocation request must be reasonable");
   1.185 +  assert_locked_or_safepoint(CodeCache_lock);
   1.186 +  CodeBlob* cb = NULL;
   1.187 +  _number_of_blobs++;
   1.188 +  while (true) {
   1.189 +    cb = (CodeBlob*)_heap->allocate(size, is_critical);
   1.190 +    if (cb != NULL) break;
   1.191 +    if (!_heap->expand_by(CodeCacheExpansionSize)) {
   1.192 +      // Expansion failed
   1.193 +      return NULL;
   1.194 +    }
   1.195 +    if (PrintCodeCacheExtension) {
   1.196 +      ResourceMark rm;
   1.197 +      tty->print_cr("code cache extended to [" INTPTR_FORMAT ", " INTPTR_FORMAT "] (" SSIZE_FORMAT " bytes)",
   1.198 +                    (intptr_t)_heap->low_boundary(), (intptr_t)_heap->high(),
   1.199 +                    (address)_heap->high() - (address)_heap->low_boundary());
   1.200 +    }
   1.201 +  }
   1.202 +  maxCodeCacheUsed = MAX2(maxCodeCacheUsed, ((address)_heap->high_boundary() -
   1.203 +                          (address)_heap->low_boundary()) - unallocated_capacity());
   1.204 +  verify_if_often();
   1.205 +  print_trace("allocation", cb, size);
   1.206 +  return cb;
   1.207 +}
   1.208 +
   1.209 +void CodeCache::free(CodeBlob* cb) {
   1.210 +  assert_locked_or_safepoint(CodeCache_lock);
   1.211 +  verify_if_often();
   1.212 +
   1.213 +  print_trace("free", cb);
   1.214 +  if (cb->is_nmethod()) {
   1.215 +    _number_of_nmethods--;
   1.216 +    if (((nmethod *)cb)->has_dependencies()) {
   1.217 +      _number_of_nmethods_with_dependencies--;
   1.218 +    }
   1.219 +  }
   1.220 +  if (cb->is_adapter_blob()) {
   1.221 +    _number_of_adapters--;
   1.222 +  }
   1.223 +  _number_of_blobs--;
   1.224 +
   1.225 +  _heap->deallocate(cb);
   1.226 +
   1.227 +  verify_if_often();
   1.228 +  assert(_number_of_blobs >= 0, "sanity check");
   1.229 +}
   1.230 +
   1.231 +
   1.232 +void CodeCache::commit(CodeBlob* cb) {
   1.233 +  // this is called by nmethod::nmethod, which must already own CodeCache_lock
   1.234 +  assert_locked_or_safepoint(CodeCache_lock);
   1.235 +  if (cb->is_nmethod()) {
   1.236 +    _number_of_nmethods++;
   1.237 +    if (((nmethod *)cb)->has_dependencies()) {
   1.238 +      _number_of_nmethods_with_dependencies++;
   1.239 +    }
   1.240 +  }
   1.241 +  if (cb->is_adapter_blob()) {
   1.242 +    _number_of_adapters++;
   1.243 +  }
   1.244 +
   1.245 +  // flush the hardware I-cache
   1.246 +  ICache::invalidate_range(cb->content_begin(), cb->content_size());
   1.247 +}
   1.248 +
   1.249 +
   1.250 +void CodeCache::flush() {
   1.251 +  assert_locked_or_safepoint(CodeCache_lock);
   1.252 +  Unimplemented();
   1.253 +}
   1.254 +
   1.255 +
   1.256 +// Iteration over CodeBlobs
   1.257 +
   1.258 +#define FOR_ALL_BLOBS(var)       for (CodeBlob *var =       first() ; var != NULL; var =       next(var) )
   1.259 +#define FOR_ALL_ALIVE_BLOBS(var) for (CodeBlob *var = alive(first()); var != NULL; var = alive(next(var)))
   1.260 +#define FOR_ALL_ALIVE_NMETHODS(var) for (nmethod *var = alive_nmethod(first()); var != NULL; var = alive_nmethod(next(var)))
   1.261 +
   1.262 +
   1.263 +bool CodeCache::contains(void *p) {
   1.264 +  // It should be ok to call contains without holding a lock
   1.265 +  return _heap->contains(p);
   1.266 +}
   1.267 +
   1.268 +
   1.269 +// This method is safe to call without holding the CodeCache_lock, as long as a dead codeblob is not
   1.270 +// looked up (i.e., one that has been marked for deletion). It only dependes on the _segmap to contain
   1.271 +// valid indices, which it will always do, as long as the CodeBlob is not in the process of being recycled.
   1.272 +CodeBlob* CodeCache::find_blob(void* start) {
   1.273 +  CodeBlob* result = find_blob_unsafe(start);
   1.274 +  if (result == NULL) return NULL;
   1.275 +  // We could potientially look up non_entrant methods
   1.276 +  guarantee(!result->is_zombie() || result->is_locked_by_vm() || is_error_reported(), "unsafe access to zombie method");
   1.277 +  return result;
   1.278 +}
   1.279 +
   1.280 +nmethod* CodeCache::find_nmethod(void* start) {
   1.281 +  CodeBlob *cb = find_blob(start);
   1.282 +  assert(cb == NULL || cb->is_nmethod(), "did not find an nmethod");
   1.283 +  return (nmethod*)cb;
   1.284 +}
   1.285 +
   1.286 +
   1.287 +void CodeCache::blobs_do(void f(CodeBlob* nm)) {
   1.288 +  assert_locked_or_safepoint(CodeCache_lock);
   1.289 +  FOR_ALL_BLOBS(p) {
   1.290 +    f(p);
   1.291 +  }
   1.292 +}
   1.293 +
   1.294 +
   1.295 +void CodeCache::nmethods_do(void f(nmethod* nm)) {
   1.296 +  assert_locked_or_safepoint(CodeCache_lock);
   1.297 +  FOR_ALL_BLOBS(nm) {
   1.298 +    if (nm->is_nmethod()) f((nmethod*)nm);
   1.299 +  }
   1.300 +}
   1.301 +
   1.302 +void CodeCache::alive_nmethods_do(void f(nmethod* nm)) {
   1.303 +  assert_locked_or_safepoint(CodeCache_lock);
   1.304 +  FOR_ALL_ALIVE_NMETHODS(nm) {
   1.305 +    f(nm);
   1.306 +  }
   1.307 +}
   1.308 +
   1.309 +int CodeCache::alignment_unit() {
   1.310 +  return (int)_heap->alignment_unit();
   1.311 +}
   1.312 +
   1.313 +
   1.314 +int CodeCache::alignment_offset() {
   1.315 +  return (int)_heap->alignment_offset();
   1.316 +}
   1.317 +
   1.318 +
   1.319 +// Mark nmethods for unloading if they contain otherwise unreachable
   1.320 +// oops.
   1.321 +void CodeCache::do_unloading(BoolObjectClosure* is_alive, bool unloading_occurred) {
   1.322 +  assert_locked_or_safepoint(CodeCache_lock);
   1.323 +  FOR_ALL_ALIVE_NMETHODS(nm) {
   1.324 +    nm->do_unloading(is_alive, unloading_occurred);
   1.325 +  }
   1.326 +}
   1.327 +
   1.328 +void CodeCache::blobs_do(CodeBlobClosure* f) {
   1.329 +  assert_locked_or_safepoint(CodeCache_lock);
   1.330 +  FOR_ALL_ALIVE_BLOBS(cb) {
   1.331 +    f->do_code_blob(cb);
   1.332 +
   1.333 +#ifdef ASSERT
   1.334 +    if (cb->is_nmethod())
   1.335 +      ((nmethod*)cb)->verify_scavenge_root_oops();
   1.336 +#endif //ASSERT
   1.337 +  }
   1.338 +}
   1.339 +
   1.340 +// Walk the list of methods which might contain non-perm oops.
   1.341 +void CodeCache::scavenge_root_nmethods_do(CodeBlobClosure* f) {
   1.342 +  assert_locked_or_safepoint(CodeCache_lock);
   1.343 +  debug_only(mark_scavenge_root_nmethods());
   1.344 +
   1.345 +  for (nmethod* cur = scavenge_root_nmethods(); cur != NULL; cur = cur->scavenge_root_link()) {
   1.346 +    debug_only(cur->clear_scavenge_root_marked());
   1.347 +    assert(cur->scavenge_root_not_marked(), "");
   1.348 +    assert(cur->on_scavenge_root_list(), "else shouldn't be on this list");
   1.349 +
   1.350 +    bool is_live = (!cur->is_zombie() && !cur->is_unloaded());
   1.351 +#ifndef PRODUCT
   1.352 +    if (TraceScavenge) {
   1.353 +      cur->print_on(tty, is_live ? "scavenge root" : "dead scavenge root"); tty->cr();
   1.354 +    }
   1.355 +#endif //PRODUCT
   1.356 +    if (is_live) {
   1.357 +      // Perform cur->oops_do(f), maybe just once per nmethod.
   1.358 +      f->do_code_blob(cur);
   1.359 +    }
   1.360 +  }
   1.361 +
   1.362 +  // Check for stray marks.
   1.363 +  debug_only(verify_perm_nmethods(NULL));
   1.364 +}
   1.365 +
   1.366 +void CodeCache::add_scavenge_root_nmethod(nmethod* nm) {
   1.367 +  assert_locked_or_safepoint(CodeCache_lock);
   1.368 +  nm->set_on_scavenge_root_list();
   1.369 +  nm->set_scavenge_root_link(_scavenge_root_nmethods);
   1.370 +  set_scavenge_root_nmethods(nm);
   1.371 +  print_trace("add_scavenge_root", nm);
   1.372 +}
   1.373 +
   1.374 +void CodeCache::drop_scavenge_root_nmethod(nmethod* nm) {
   1.375 +  assert_locked_or_safepoint(CodeCache_lock);
   1.376 +  print_trace("drop_scavenge_root", nm);
   1.377 +  nmethod* last = NULL;
   1.378 +  nmethod* cur = scavenge_root_nmethods();
   1.379 +  while (cur != NULL) {
   1.380 +    nmethod* next = cur->scavenge_root_link();
   1.381 +    if (cur == nm) {
   1.382 +      if (last != NULL)
   1.383 +            last->set_scavenge_root_link(next);
   1.384 +      else  set_scavenge_root_nmethods(next);
   1.385 +      nm->set_scavenge_root_link(NULL);
   1.386 +      nm->clear_on_scavenge_root_list();
   1.387 +      return;
   1.388 +    }
   1.389 +    last = cur;
   1.390 +    cur = next;
   1.391 +  }
   1.392 +  assert(false, "should have been on list");
   1.393 +}
   1.394 +
   1.395 +void CodeCache::prune_scavenge_root_nmethods() {
   1.396 +  assert_locked_or_safepoint(CodeCache_lock);
   1.397 +  debug_only(mark_scavenge_root_nmethods());
   1.398 +
   1.399 +  nmethod* last = NULL;
   1.400 +  nmethod* cur = scavenge_root_nmethods();
   1.401 +  while (cur != NULL) {
   1.402 +    nmethod* next = cur->scavenge_root_link();
   1.403 +    debug_only(cur->clear_scavenge_root_marked());
   1.404 +    assert(cur->scavenge_root_not_marked(), "");
   1.405 +    assert(cur->on_scavenge_root_list(), "else shouldn't be on this list");
   1.406 +
   1.407 +    if (!cur->is_zombie() && !cur->is_unloaded()
   1.408 +        && cur->detect_scavenge_root_oops()) {
   1.409 +      // Keep it.  Advance 'last' to prevent deletion.
   1.410 +      last = cur;
   1.411 +    } else {
   1.412 +      // Prune it from the list, so we don't have to look at it any more.
   1.413 +      print_trace("prune_scavenge_root", cur);
   1.414 +      cur->set_scavenge_root_link(NULL);
   1.415 +      cur->clear_on_scavenge_root_list();
   1.416 +      if (last != NULL)
   1.417 +            last->set_scavenge_root_link(next);
   1.418 +      else  set_scavenge_root_nmethods(next);
   1.419 +    }
   1.420 +    cur = next;
   1.421 +  }
   1.422 +
   1.423 +  // Check for stray marks.
   1.424 +  debug_only(verify_perm_nmethods(NULL));
   1.425 +}
   1.426 +
   1.427 +#ifndef PRODUCT
   1.428 +void CodeCache::asserted_non_scavengable_nmethods_do(CodeBlobClosure* f) {
   1.429 +  // While we are here, verify the integrity of the list.
   1.430 +  mark_scavenge_root_nmethods();
   1.431 +  for (nmethod* cur = scavenge_root_nmethods(); cur != NULL; cur = cur->scavenge_root_link()) {
   1.432 +    assert(cur->on_scavenge_root_list(), "else shouldn't be on this list");
   1.433 +    cur->clear_scavenge_root_marked();
   1.434 +  }
   1.435 +  verify_perm_nmethods(f);
   1.436 +}
   1.437 +
   1.438 +// Temporarily mark nmethods that are claimed to be on the non-perm list.
   1.439 +void CodeCache::mark_scavenge_root_nmethods() {
   1.440 +  FOR_ALL_ALIVE_BLOBS(cb) {
   1.441 +    if (cb->is_nmethod()) {
   1.442 +      nmethod *nm = (nmethod*)cb;
   1.443 +      assert(nm->scavenge_root_not_marked(), "clean state");
   1.444 +      if (nm->on_scavenge_root_list())
   1.445 +        nm->set_scavenge_root_marked();
   1.446 +    }
   1.447 +  }
   1.448 +}
   1.449 +
   1.450 +// If the closure is given, run it on the unlisted nmethods.
   1.451 +// Also make sure that the effects of mark_scavenge_root_nmethods is gone.
   1.452 +void CodeCache::verify_perm_nmethods(CodeBlobClosure* f_or_null) {
   1.453 +  FOR_ALL_ALIVE_BLOBS(cb) {
   1.454 +    bool call_f = (f_or_null != NULL);
   1.455 +    if (cb->is_nmethod()) {
   1.456 +      nmethod *nm = (nmethod*)cb;
   1.457 +      assert(nm->scavenge_root_not_marked(), "must be already processed");
   1.458 +      if (nm->on_scavenge_root_list())
   1.459 +        call_f = false;  // don't show this one to the client
   1.460 +      nm->verify_scavenge_root_oops();
   1.461 +    } else {
   1.462 +      call_f = false;   // not an nmethod
   1.463 +    }
   1.464 +    if (call_f)  f_or_null->do_code_blob(cb);
   1.465 +  }
   1.466 +}
   1.467 +#endif //PRODUCT
   1.468 +
   1.469 +
   1.470 +void CodeCache::gc_prologue() {
   1.471 +  assert(!nmethod::oops_do_marking_is_active(), "oops_do_marking_epilogue must be called");
   1.472 +}
   1.473 +
   1.474 +void CodeCache::gc_epilogue() {
   1.475 +  assert_locked_or_safepoint(CodeCache_lock);
   1.476 +  FOR_ALL_ALIVE_BLOBS(cb) {
   1.477 +    if (cb->is_nmethod()) {
   1.478 +      nmethod *nm = (nmethod*)cb;
   1.479 +      assert(!nm->is_unloaded(), "Tautology");
   1.480 +      if (needs_cache_clean()) {
   1.481 +        nm->cleanup_inline_caches();
   1.482 +      }
   1.483 +      DEBUG_ONLY(nm->verify());
   1.484 +      nm->fix_oop_relocations();
   1.485 +    }
   1.486 +  }
   1.487 +  set_needs_cache_clean(false);
   1.488 +  prune_scavenge_root_nmethods();
   1.489 +  assert(!nmethod::oops_do_marking_is_active(), "oops_do_marking_prologue must be called");
   1.490 +
   1.491 +#ifdef ASSERT
   1.492 +  // make sure that we aren't leaking icholders
   1.493 +  int count = 0;
   1.494 +  FOR_ALL_BLOBS(cb) {
   1.495 +    if (cb->is_nmethod()) {
   1.496 +      RelocIterator iter((nmethod*)cb);
   1.497 +      while(iter.next()) {
   1.498 +        if (iter.type() == relocInfo::virtual_call_type) {
   1.499 +          if (CompiledIC::is_icholder_call_site(iter.virtual_call_reloc())) {
   1.500 +            CompiledIC *ic = CompiledIC_at(iter.reloc());
   1.501 +            if (TraceCompiledIC) {
   1.502 +              tty->print("noticed icholder " INTPTR_FORMAT " ", p2i(ic->cached_icholder()));
   1.503 +              ic->print();
   1.504 +            }
   1.505 +            assert(ic->cached_icholder() != NULL, "must be non-NULL");
   1.506 +            count++;
   1.507 +          }
   1.508 +        }
   1.509 +      }
   1.510 +    }
   1.511 +  }
   1.512 +
   1.513 +  assert(count + InlineCacheBuffer::pending_icholder_count() + CompiledICHolder::live_not_claimed_count() ==
   1.514 +         CompiledICHolder::live_count(), "must agree");
   1.515 +#endif
   1.516 +}
   1.517 +
   1.518 +
   1.519 +void CodeCache::verify_oops() {
   1.520 +  MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
   1.521 +  VerifyOopClosure voc;
   1.522 +  FOR_ALL_ALIVE_BLOBS(cb) {
   1.523 +    if (cb->is_nmethod()) {
   1.524 +      nmethod *nm = (nmethod*)cb;
   1.525 +      nm->oops_do(&voc);
   1.526 +      nm->verify_oop_relocations();
   1.527 +    }
   1.528 +  }
   1.529 +}
   1.530 +
   1.531 +
   1.532 +address CodeCache::first_address() {
   1.533 +  assert_locked_or_safepoint(CodeCache_lock);
   1.534 +  return (address)_heap->low_boundary();
   1.535 +}
   1.536 +
   1.537 +
   1.538 +address CodeCache::last_address() {
   1.539 +  assert_locked_or_safepoint(CodeCache_lock);
   1.540 +  return (address)_heap->high();
   1.541 +}
   1.542 +
   1.543 +/**
   1.544 + * Returns the reverse free ratio. E.g., if 25% (1/4) of the code cache
   1.545 + * is free, reverse_free_ratio() returns 4.
   1.546 + */
   1.547 +double CodeCache::reverse_free_ratio() {
   1.548 +  double unallocated_capacity = (double)(CodeCache::unallocated_capacity() - CodeCacheMinimumFreeSpace);
   1.549 +  double max_capacity = (double)CodeCache::max_capacity();
   1.550 +  return max_capacity / unallocated_capacity;
   1.551 +}
   1.552 +
   1.553 +void icache_init();
   1.554 +
   1.555 +void CodeCache::initialize() {
   1.556 +  assert(CodeCacheSegmentSize >= (uintx)CodeEntryAlignment, "CodeCacheSegmentSize must be large enough to align entry points");
   1.557 +#ifdef COMPILER2
   1.558 +  assert(CodeCacheSegmentSize >= (uintx)OptoLoopAlignment,  "CodeCacheSegmentSize must be large enough to align inner loops");
   1.559 +#endif
   1.560 +  assert(CodeCacheSegmentSize >= sizeof(jdouble),    "CodeCacheSegmentSize must be large enough to align constants");
   1.561 +  // This was originally just a check of the alignment, causing failure, instead, round
   1.562 +  // the code cache to the page size.  In particular, Solaris is moving to a larger
   1.563 +  // default page size.
   1.564 +  CodeCacheExpansionSize = round_to(CodeCacheExpansionSize, os::vm_page_size());
   1.565 +  InitialCodeCacheSize = round_to(InitialCodeCacheSize, os::vm_page_size());
   1.566 +  ReservedCodeCacheSize = round_to(ReservedCodeCacheSize, os::vm_page_size());
   1.567 +  if (!_heap->reserve(ReservedCodeCacheSize, InitialCodeCacheSize, CodeCacheSegmentSize)) {
   1.568 +    vm_exit_during_initialization("Could not reserve enough space for code cache");
   1.569 +  }
   1.570 +
   1.571 +  MemoryService::add_code_heap_memory_pool(_heap);
   1.572 +
   1.573 +  // Initialize ICache flush mechanism
   1.574 +  // This service is needed for os::register_code_area
   1.575 +  icache_init();
   1.576 +
   1.577 +  // Give OS a chance to register generated code area.
   1.578 +  // This is used on Windows 64 bit platforms to register
   1.579 +  // Structured Exception Handlers for our generated code.
   1.580 +  os::register_code_area(_heap->low_boundary(), _heap->high_boundary());
   1.581 +}
   1.582 +
   1.583 +
   1.584 +void codeCache_init() {
   1.585 +  CodeCache::initialize();
   1.586 +}
   1.587 +
   1.588 +//------------------------------------------------------------------------------------------------
   1.589 +
   1.590 +int CodeCache::number_of_nmethods_with_dependencies() {
   1.591 +  return _number_of_nmethods_with_dependencies;
   1.592 +}
   1.593 +
   1.594 +void CodeCache::clear_inline_caches() {
   1.595 +  assert_locked_or_safepoint(CodeCache_lock);
   1.596 +  FOR_ALL_ALIVE_NMETHODS(nm) {
   1.597 +    nm->clear_inline_caches();
   1.598 +  }
   1.599 +}
   1.600 +
   1.601 +#ifndef PRODUCT
   1.602 +// used to keep track of how much time is spent in mark_for_deoptimization
   1.603 +static elapsedTimer dependentCheckTime;
   1.604 +static int dependentCheckCount = 0;
   1.605 +#endif // PRODUCT
   1.606 +
   1.607 +
   1.608 +int CodeCache::mark_for_deoptimization(DepChange& changes) {
   1.609 +  MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
   1.610 +
   1.611 +#ifndef PRODUCT
   1.612 +  dependentCheckTime.start();
   1.613 +  dependentCheckCount++;
   1.614 +#endif // PRODUCT
   1.615 +
   1.616 +  int number_of_marked_CodeBlobs = 0;
   1.617 +
   1.618 +  // search the hierarchy looking for nmethods which are affected by the loading of this class
   1.619 +
   1.620 +  // then search the interfaces this class implements looking for nmethods
   1.621 +  // which might be dependent of the fact that an interface only had one
   1.622 +  // implementor.
   1.623 +
   1.624 +  { No_Safepoint_Verifier nsv;
   1.625 +    for (DepChange::ContextStream str(changes, nsv); str.next(); ) {
   1.626 +      Klass* d = str.klass();
   1.627 +      number_of_marked_CodeBlobs += InstanceKlass::cast(d)->mark_dependent_nmethods(changes);
   1.628 +    }
   1.629 +  }
   1.630 +
   1.631 +  if (VerifyDependencies) {
   1.632 +    // Turn off dependency tracing while actually testing deps.
   1.633 +    NOT_PRODUCT( FlagSetting fs(TraceDependencies, false) );
   1.634 +    FOR_ALL_ALIVE_NMETHODS(nm) {
   1.635 +      if (!nm->is_marked_for_deoptimization() &&
   1.636 +          nm->check_all_dependencies()) {
   1.637 +        ResourceMark rm;
   1.638 +        tty->print_cr("Should have been marked for deoptimization:");
   1.639 +        changes.print();
   1.640 +        nm->print();
   1.641 +        nm->print_dependencies();
   1.642 +      }
   1.643 +    }
   1.644 +  }
   1.645 +
   1.646 +#ifndef PRODUCT
   1.647 +  dependentCheckTime.stop();
   1.648 +#endif // PRODUCT
   1.649 +
   1.650 +  return number_of_marked_CodeBlobs;
   1.651 +}
   1.652 +
   1.653 +
   1.654 +#ifdef HOTSWAP
   1.655 +int CodeCache::mark_for_evol_deoptimization(instanceKlassHandle dependee) {
   1.656 +  MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
   1.657 +  int number_of_marked_CodeBlobs = 0;
   1.658 +
   1.659 +  // Deoptimize all methods of the evolving class itself
   1.660 +  Array<Method*>* old_methods = dependee->methods();
   1.661 +  for (int i = 0; i < old_methods->length(); i++) {
   1.662 +    ResourceMark rm;
   1.663 +    Method* old_method = old_methods->at(i);
   1.664 +    nmethod *nm = old_method->code();
   1.665 +    if (nm != NULL) {
   1.666 +      nm->mark_for_deoptimization();
   1.667 +      number_of_marked_CodeBlobs++;
   1.668 +    }
   1.669 +  }
   1.670 +
   1.671 +  FOR_ALL_ALIVE_NMETHODS(nm) {
   1.672 +    if (nm->is_marked_for_deoptimization()) {
   1.673 +      // ...Already marked in the previous pass; don't count it again.
   1.674 +    } else if (nm->is_evol_dependent_on(dependee())) {
   1.675 +      ResourceMark rm;
   1.676 +      nm->mark_for_deoptimization();
   1.677 +      number_of_marked_CodeBlobs++;
   1.678 +    } else  {
   1.679 +      // flush caches in case they refer to a redefined Method*
   1.680 +      nm->clear_inline_caches();
   1.681 +    }
   1.682 +  }
   1.683 +
   1.684 +  return number_of_marked_CodeBlobs;
   1.685 +}
   1.686 +#endif // HOTSWAP
   1.687 +
   1.688 +
   1.689 +// Deoptimize all methods
   1.690 +void CodeCache::mark_all_nmethods_for_deoptimization() {
   1.691 +  MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
   1.692 +  FOR_ALL_ALIVE_NMETHODS(nm) {
   1.693 +    nm->mark_for_deoptimization();
   1.694 +  }
   1.695 +}
   1.696 +
   1.697 +
   1.698 +int CodeCache::mark_for_deoptimization(Method* dependee) {
   1.699 +  MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
   1.700 +  int number_of_marked_CodeBlobs = 0;
   1.701 +
   1.702 +  FOR_ALL_ALIVE_NMETHODS(nm) {
   1.703 +    if (nm->is_dependent_on_method(dependee)) {
   1.704 +      ResourceMark rm;
   1.705 +      nm->mark_for_deoptimization();
   1.706 +      number_of_marked_CodeBlobs++;
   1.707 +    }
   1.708 +  }
   1.709 +
   1.710 +  return number_of_marked_CodeBlobs;
   1.711 +}
   1.712 +
   1.713 +void CodeCache::make_marked_nmethods_zombies() {
   1.714 +  assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
   1.715 +  FOR_ALL_ALIVE_NMETHODS(nm) {
   1.716 +    if (nm->is_marked_for_deoptimization()) {
   1.717 +
   1.718 +      // If the nmethod has already been made non-entrant and it can be converted
   1.719 +      // then zombie it now. Otherwise make it non-entrant and it will eventually
   1.720 +      // be zombied when it is no longer seen on the stack. Note that the nmethod
   1.721 +      // might be "entrant" and not on the stack and so could be zombied immediately
   1.722 +      // but we can't tell because we don't track it on stack until it becomes
   1.723 +      // non-entrant.
   1.724 +
   1.725 +      if (nm->is_not_entrant() && nm->can_not_entrant_be_converted()) {
   1.726 +        nm->make_zombie();
   1.727 +      } else {
   1.728 +        nm->make_not_entrant();
   1.729 +      }
   1.730 +    }
   1.731 +  }
   1.732 +}
   1.733 +
   1.734 +void CodeCache::make_marked_nmethods_not_entrant() {
   1.735 +  assert_locked_or_safepoint(CodeCache_lock);
   1.736 +  FOR_ALL_ALIVE_NMETHODS(nm) {
   1.737 +    if (nm->is_marked_for_deoptimization()) {
   1.738 +      nm->make_not_entrant();
   1.739 +    }
   1.740 +  }
   1.741 +}
   1.742 +
   1.743 +void CodeCache::verify() {
   1.744 +  _heap->verify();
   1.745 +  FOR_ALL_ALIVE_BLOBS(p) {
   1.746 +    p->verify();
   1.747 +  }
   1.748 +}
   1.749 +
   1.750 +void CodeCache::report_codemem_full() {
   1.751 +  _codemem_full_count++;
   1.752 +  EventCodeCacheFull event;
   1.753 +  if (event.should_commit()) {
   1.754 +    event.set_startAddress((u8)low_bound());
   1.755 +    event.set_commitedTopAddress((u8)high());
   1.756 +    event.set_reservedTopAddress((u8)high_bound());
   1.757 +    event.set_entryCount(nof_blobs());
   1.758 +    event.set_methodCount(nof_nmethods());
   1.759 +    event.set_adaptorCount(nof_adapters());
   1.760 +    event.set_unallocatedCapacity(unallocated_capacity()/K);
   1.761 +    event.set_fullCount(_codemem_full_count);
   1.762 +    event.commit();
   1.763 +  }
   1.764 +}
   1.765 +
   1.766 +//------------------------------------------------------------------------------------------------
   1.767 +// Non-product version
   1.768 +
   1.769 +#ifndef PRODUCT
   1.770 +
   1.771 +void CodeCache::verify_if_often() {
   1.772 +  if (VerifyCodeCacheOften) {
   1.773 +    _heap->verify();
   1.774 +  }
   1.775 +}
   1.776 +
   1.777 +void CodeCache::print_trace(const char* event, CodeBlob* cb, int size) {
   1.778 +  if (PrintCodeCache2) {  // Need to add a new flag
   1.779 +    ResourceMark rm;
   1.780 +    if (size == 0)  size = cb->size();
   1.781 +    tty->print_cr("CodeCache %s:  addr: " INTPTR_FORMAT ", size: 0x%x", event, p2i(cb), size);
   1.782 +  }
   1.783 +}
   1.784 +
   1.785 +void CodeCache::print_internals() {
   1.786 +  int nmethodCount = 0;
   1.787 +  int runtimeStubCount = 0;
   1.788 +  int adapterCount = 0;
   1.789 +  int deoptimizationStubCount = 0;
   1.790 +  int uncommonTrapStubCount = 0;
   1.791 +  int bufferBlobCount = 0;
   1.792 +  int total = 0;
   1.793 +  int nmethodAlive = 0;
   1.794 +  int nmethodNotEntrant = 0;
   1.795 +  int nmethodZombie = 0;
   1.796 +  int nmethodUnloaded = 0;
   1.797 +  int nmethodJava = 0;
   1.798 +  int nmethodNative = 0;
   1.799 +  int maxCodeSize = 0;
   1.800 +  ResourceMark rm;
   1.801 +
   1.802 +  CodeBlob *cb;
   1.803 +  for (cb = first(); cb != NULL; cb = next(cb)) {
   1.804 +    total++;
   1.805 +    if (cb->is_nmethod()) {
   1.806 +      nmethod* nm = (nmethod*)cb;
   1.807 +
   1.808 +      if (Verbose && nm->method() != NULL) {
   1.809 +        ResourceMark rm;
   1.810 +        char *method_name = nm->method()->name_and_sig_as_C_string();
   1.811 +        tty->print("%s", method_name);
   1.812 +        if(nm->is_alive()) { tty->print_cr(" alive"); }
   1.813 +        if(nm->is_not_entrant()) { tty->print_cr(" not-entrant"); }
   1.814 +        if(nm->is_zombie()) { tty->print_cr(" zombie"); }
   1.815 +      }
   1.816 +
   1.817 +      nmethodCount++;
   1.818 +
   1.819 +      if(nm->is_alive()) { nmethodAlive++; }
   1.820 +      if(nm->is_not_entrant()) { nmethodNotEntrant++; }
   1.821 +      if(nm->is_zombie()) { nmethodZombie++; }
   1.822 +      if(nm->is_unloaded()) { nmethodUnloaded++; }
   1.823 +      if(nm->is_native_method()) { nmethodNative++; }
   1.824 +
   1.825 +      if(nm->method() != NULL && nm->is_java_method()) {
   1.826 +        nmethodJava++;
   1.827 +        if (nm->insts_size() > maxCodeSize) {
   1.828 +          maxCodeSize = nm->insts_size();
   1.829 +        }
   1.830 +      }
   1.831 +    } else if (cb->is_runtime_stub()) {
   1.832 +      runtimeStubCount++;
   1.833 +    } else if (cb->is_deoptimization_stub()) {
   1.834 +      deoptimizationStubCount++;
   1.835 +    } else if (cb->is_uncommon_trap_stub()) {
   1.836 +      uncommonTrapStubCount++;
   1.837 +    } else if (cb->is_adapter_blob()) {
   1.838 +      adapterCount++;
   1.839 +    } else if (cb->is_buffer_blob()) {
   1.840 +      bufferBlobCount++;
   1.841 +    }
   1.842 +  }
   1.843 +
   1.844 +  int bucketSize = 512;
   1.845 +  int bucketLimit = maxCodeSize / bucketSize + 1;
   1.846 +  int *buckets = NEW_C_HEAP_ARRAY(int, bucketLimit, mtCode);
   1.847 +  memset(buckets,0,sizeof(int) * bucketLimit);
   1.848 +
   1.849 +  for (cb = first(); cb != NULL; cb = next(cb)) {
   1.850 +    if (cb->is_nmethod()) {
   1.851 +      nmethod* nm = (nmethod*)cb;
   1.852 +      if(nm->is_java_method()) {
   1.853 +        buckets[nm->insts_size() / bucketSize]++;
   1.854 +      }
   1.855 +    }
   1.856 +  }
   1.857 +  tty->print_cr("Code Cache Entries (total of %d)",total);
   1.858 +  tty->print_cr("-------------------------------------------------");
   1.859 +  tty->print_cr("nmethods: %d",nmethodCount);
   1.860 +  tty->print_cr("\talive: %d",nmethodAlive);
   1.861 +  tty->print_cr("\tnot_entrant: %d",nmethodNotEntrant);
   1.862 +  tty->print_cr("\tzombie: %d",nmethodZombie);
   1.863 +  tty->print_cr("\tunloaded: %d",nmethodUnloaded);
   1.864 +  tty->print_cr("\tjava: %d",nmethodJava);
   1.865 +  tty->print_cr("\tnative: %d",nmethodNative);
   1.866 +  tty->print_cr("runtime_stubs: %d",runtimeStubCount);
   1.867 +  tty->print_cr("adapters: %d",adapterCount);
   1.868 +  tty->print_cr("buffer blobs: %d",bufferBlobCount);
   1.869 +  tty->print_cr("deoptimization_stubs: %d",deoptimizationStubCount);
   1.870 +  tty->print_cr("uncommon_traps: %d",uncommonTrapStubCount);
   1.871 +  tty->print_cr("\nnmethod size distribution (non-zombie java)");
   1.872 +  tty->print_cr("-------------------------------------------------");
   1.873 +
   1.874 +  for(int i=0; i<bucketLimit; i++) {
   1.875 +    if(buckets[i] != 0) {
   1.876 +      tty->print("%d - %d bytes",i*bucketSize,(i+1)*bucketSize);
   1.877 +      tty->fill_to(40);
   1.878 +      tty->print_cr("%d",buckets[i]);
   1.879 +    }
   1.880 +  }
   1.881 +
   1.882 +  FREE_C_HEAP_ARRAY(int, buckets, mtCode);
   1.883 +}
   1.884 +
   1.885 +#endif // !PRODUCT
   1.886 +
   1.887 +void CodeCache::print() {
   1.888 +  print_summary(tty);
   1.889 +
   1.890 +#ifndef PRODUCT
   1.891 +  if (!Verbose) return;
   1.892 +
   1.893 +  CodeBlob_sizes live;
   1.894 +  CodeBlob_sizes dead;
   1.895 +
   1.896 +  FOR_ALL_BLOBS(p) {
   1.897 +    if (!p->is_alive()) {
   1.898 +      dead.add(p);
   1.899 +    } else {
   1.900 +      live.add(p);
   1.901 +    }
   1.902 +  }
   1.903 +
   1.904 +  tty->print_cr("CodeCache:");
   1.905 +
   1.906 +  tty->print_cr("nmethod dependency checking time %f, per dependent %f", dependentCheckTime.seconds(),
   1.907 +                dependentCheckTime.seconds() / dependentCheckCount);
   1.908 +
   1.909 +  if (!live.is_empty()) {
   1.910 +    live.print("live");
   1.911 +  }
   1.912 +  if (!dead.is_empty()) {
   1.913 +    dead.print("dead");
   1.914 +  }
   1.915 +
   1.916 +
   1.917 +  if (WizardMode) {
   1.918 +     // print the oop_map usage
   1.919 +    int code_size = 0;
   1.920 +    int number_of_blobs = 0;
   1.921 +    int number_of_oop_maps = 0;
   1.922 +    int map_size = 0;
   1.923 +    FOR_ALL_BLOBS(p) {
   1.924 +      if (p->is_alive()) {
   1.925 +        number_of_blobs++;
   1.926 +        code_size += p->code_size();
   1.927 +        OopMapSet* set = p->oop_maps();
   1.928 +        if (set != NULL) {
   1.929 +          number_of_oop_maps += set->size();
   1.930 +          map_size           += set->heap_size();
   1.931 +        }
   1.932 +      }
   1.933 +    }
   1.934 +    tty->print_cr("OopMaps");
   1.935 +    tty->print_cr("  #blobs    = %d", number_of_blobs);
   1.936 +    tty->print_cr("  code size = %d", code_size);
   1.937 +    tty->print_cr("  #oop_maps = %d", number_of_oop_maps);
   1.938 +    tty->print_cr("  map size  = %d", map_size);
   1.939 +  }
   1.940 +
   1.941 +#endif // !PRODUCT
   1.942 +}
   1.943 +
   1.944 +void CodeCache::print_summary(outputStream* st, bool detailed) {
   1.945 +  size_t total = (_heap->high_boundary() - _heap->low_boundary());
   1.946 +  st->print_cr("CodeCache: size=" SIZE_FORMAT "Kb used=" SIZE_FORMAT
   1.947 +               "Kb max_used=" SIZE_FORMAT "Kb free=" SIZE_FORMAT "Kb",
   1.948 +               total/K, (total - unallocated_capacity())/K,
   1.949 +               maxCodeCacheUsed/K, unallocated_capacity()/K);
   1.950 +
   1.951 +  if (detailed) {
   1.952 +    st->print_cr(" bounds [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT "]",
   1.953 +                 p2i(_heap->low_boundary()),
   1.954 +                 p2i(_heap->high()),
   1.955 +                 p2i(_heap->high_boundary()));
   1.956 +    st->print_cr(" total_blobs=" UINT32_FORMAT " nmethods=" UINT32_FORMAT
   1.957 +                 " adapters=" UINT32_FORMAT,
   1.958 +                 nof_blobs(), nof_nmethods(), nof_adapters());
   1.959 +    st->print_cr(" compilation: %s", CompileBroker::should_compile_new_jobs() ?
   1.960 +                 "enabled" : Arguments::mode() == Arguments::_int ?
   1.961 +                 "disabled (interpreter mode)" :
   1.962 +                 "disabled (not enough contiguous free space left)");
   1.963 +  }
   1.964 +}
   1.965 +
   1.966 +void CodeCache::log_state(outputStream* st) {
   1.967 +  st->print(" total_blobs='" UINT32_FORMAT "' nmethods='" UINT32_FORMAT "'"
   1.968 +            " adapters='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'",
   1.969 +            nof_blobs(), nof_nmethods(), nof_adapters(),
   1.970 +            unallocated_capacity());
   1.971 +}
   1.972 +

mercurial