codeCache.cpp revision 2200
0N/A/*
1472N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#include "precompiled.hpp"
1879N/A#include "code/codeBlob.hpp"
1879N/A#include "code/codeCache.hpp"
1879N/A#include "code/dependencies.hpp"
1879N/A#include "code/nmethod.hpp"
1879N/A#include "code/pcDesc.hpp"
1879N/A#include "gc_implementation/shared/markSweep.hpp"
1879N/A#include "memory/allocation.inline.hpp"
1879N/A#include "memory/gcLocker.hpp"
1879N/A#include "memory/iterator.hpp"
1879N/A#include "memory/resourceArea.hpp"
1879N/A#include "oops/methodOop.hpp"
1879N/A#include "oops/objArrayOop.hpp"
1879N/A#include "oops/oop.inline.hpp"
1879N/A#include "runtime/handles.inline.hpp"
1879N/A#include "runtime/icache.hpp"
1879N/A#include "runtime/java.hpp"
1879N/A#include "runtime/mutexLocker.hpp"
1879N/A#include "services/memoryService.hpp"
1879N/A#include "utilities/xmlstream.hpp"
0N/A
0N/A// Helper class for printing in CodeCache
0N/A
0N/Aclass CodeBlob_sizes {
0N/A private:
0N/A int count;
0N/A int total_size;
0N/A int header_size;
0N/A int code_size;
0N/A int stub_size;
0N/A int relocation_size;
0N/A int scopes_oop_size;
0N/A int scopes_data_size;
0N/A int scopes_pcs_size;
0N/A
0N/A public:
0N/A CodeBlob_sizes() {
0N/A count = 0;
0N/A total_size = 0;
0N/A header_size = 0;
0N/A code_size = 0;
0N/A stub_size = 0;
0N/A relocation_size = 0;
0N/A scopes_oop_size = 0;
0N/A scopes_data_size = 0;
0N/A scopes_pcs_size = 0;
0N/A }
0N/A
0N/A int total() { return total_size; }
0N/A bool is_empty() { return count == 0; }
0N/A
0N/A void print(const char* title) {
0N/A tty->print_cr(" #%d %s = %dK (hdr %d%%, loc %d%%, code %d%%, stub %d%%, [oops %d%%, data %d%%, pcs %d%%])",
0N/A count,
0N/A title,
0N/A total() / K,
0N/A header_size * 100 / total_size,
0N/A relocation_size * 100 / total_size,
0N/A code_size * 100 / total_size,
0N/A stub_size * 100 / total_size,
0N/A scopes_oop_size * 100 / total_size,
0N/A scopes_data_size * 100 / total_size,
0N/A scopes_pcs_size * 100 / total_size);
0N/A }
0N/A
0N/A void add(CodeBlob* cb) {
0N/A count++;
0N/A total_size += cb->size();
0N/A header_size += cb->header_size();
0N/A relocation_size += cb->relocation_size();
0N/A if (cb->is_nmethod()) {
1483N/A nmethod* nm = cb->as_nmethod_or_null();
1668N/A code_size += nm->insts_size();
0N/A stub_size += nm->stub_size();
0N/A
1483N/A scopes_oop_size += nm->oops_size();
0N/A scopes_data_size += nm->scopes_data_size();
0N/A scopes_pcs_size += nm->scopes_pcs_size();
0N/A } else {
1668N/A code_size += cb->code_size();
0N/A }
0N/A }
0N/A};
0N/A
0N/A
0N/A// CodeCache implementation
0N/A
0N/ACodeHeap * CodeCache::_heap = new CodeHeap();
0N/Aint CodeCache::_number_of_blobs = 0;
1564N/Aint CodeCache::_number_of_adapters = 0;
1564N/Aint CodeCache::_number_of_nmethods = 0;
0N/Aint CodeCache::_number_of_nmethods_with_dependencies = 0;
0N/Abool CodeCache::_needs_cache_clean = false;
989N/Anmethod* CodeCache::_scavenge_root_nmethods = NULL;
1202N/Anmethod* CodeCache::_saved_nmethods = NULL;
0N/A
0N/A
0N/ACodeBlob* CodeCache::first() {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A return (CodeBlob*)_heap->first();
0N/A}
0N/A
0N/A
0N/ACodeBlob* CodeCache::next(CodeBlob* cb) {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A return (CodeBlob*)_heap->next(cb);
0N/A}
0N/A
0N/A
0N/ACodeBlob* CodeCache::alive(CodeBlob *cb) {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A while (cb != NULL && !cb->is_alive()) cb = next(cb);
0N/A return cb;
0N/A}
0N/A
0N/A
0N/Anmethod* CodeCache::alive_nmethod(CodeBlob* cb) {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A while (cb != NULL && (!cb->is_alive() || !cb->is_nmethod())) cb = next(cb);
0N/A return (nmethod*)cb;
0N/A}
0N/A
1458N/Anmethod* CodeCache::first_nmethod() {
1458N/A assert_locked_or_safepoint(CodeCache_lock);
1458N/A CodeBlob* cb = first();
1458N/A while (cb != NULL && !cb->is_nmethod()) {
1458N/A cb = next(cb);
1458N/A }
1458N/A return (nmethod*)cb;
1458N/A}
1458N/A
1458N/Anmethod* CodeCache::next_nmethod (CodeBlob* cb) {
1458N/A assert_locked_or_safepoint(CodeCache_lock);
1458N/A cb = next(cb);
1458N/A while (cb != NULL && !cb->is_nmethod()) {
1458N/A cb = next(cb);
1458N/A }
1458N/A return (nmethod*)cb;
1458N/A}
0N/A
0N/ACodeBlob* CodeCache::allocate(int size) {
0N/A // Do not seize the CodeCache lock here--if the caller has not
0N/A // already done so, we are going to lose bigtime, since the code
0N/A // cache will contain a garbage CodeBlob until the caller can
0N/A // run the constructor for the CodeBlob subclass he is busy
0N/A // instantiating.
0N/A guarantee(size >= 0, "allocation request must be reasonable");
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A CodeBlob* cb = NULL;
0N/A _number_of_blobs++;
0N/A while (true) {
0N/A cb = (CodeBlob*)_heap->allocate(size);
0N/A if (cb != NULL) break;
0N/A if (!_heap->expand_by(CodeCacheExpansionSize)) {
0N/A // Expansion failed
0N/A return NULL;
0N/A }
0N/A if (PrintCodeCacheExtension) {
0N/A ResourceMark rm;
0N/A tty->print_cr("code cache extended to [" INTPTR_FORMAT ", " INTPTR_FORMAT "] (%d bytes)",
0N/A (intptr_t)_heap->begin(), (intptr_t)_heap->end(),
0N/A (address)_heap->end() - (address)_heap->begin());
0N/A }
0N/A }
0N/A verify_if_often();
989N/A print_trace("allocation", cb, size);
0N/A return cb;
0N/A}
0N/A
0N/Avoid CodeCache::free(CodeBlob* cb) {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A verify_if_often();
0N/A
989N/A print_trace("free", cb);
1564N/A if (cb->is_nmethod()) {
1564N/A _number_of_nmethods--;
1564N/A if (((nmethod *)cb)->has_dependencies()) {
1564N/A _number_of_nmethods_with_dependencies--;
1564N/A }
1564N/A }
1564N/A if (cb->is_adapter_blob()) {
1564N/A _number_of_adapters--;
0N/A }
0N/A _number_of_blobs--;
0N/A
0N/A _heap->deallocate(cb);
0N/A
0N/A verify_if_often();
0N/A assert(_number_of_blobs >= 0, "sanity check");
0N/A}
0N/A
0N/A
0N/Avoid CodeCache::commit(CodeBlob* cb) {
0N/A // this is called by nmethod::nmethod, which must already own CodeCache_lock
0N/A assert_locked_or_safepoint(CodeCache_lock);
1564N/A if (cb->is_nmethod()) {
1564N/A _number_of_nmethods++;
1564N/A if (((nmethod *)cb)->has_dependencies()) {
1564N/A _number_of_nmethods_with_dependencies++;
1564N/A }
0N/A }
1564N/A if (cb->is_adapter_blob()) {
1564N/A _number_of_adapters++;
1564N/A }
1564N/A
0N/A // flush the hardware I-cache
1668N/A ICache::invalidate_range(cb->content_begin(), cb->content_size());
0N/A}
0N/A
0N/A
0N/Avoid CodeCache::flush() {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A Unimplemented();
0N/A}
0N/A
0N/A
0N/A// Iteration over CodeBlobs
0N/A
0N/A#define FOR_ALL_BLOBS(var) for (CodeBlob *var = first() ; var != NULL; var = next(var) )
0N/A#define FOR_ALL_ALIVE_BLOBS(var) for (CodeBlob *var = alive(first()); var != NULL; var = alive(next(var)))
0N/A#define FOR_ALL_ALIVE_NMETHODS(var) for (nmethod *var = alive_nmethod(first()); var != NULL; var = alive_nmethod(next(var)))
0N/A
0N/A
0N/Abool CodeCache::contains(void *p) {
0N/A // It should be ok to call contains without holding a lock
0N/A return _heap->contains(p);
0N/A}
0N/A
0N/A
0N/A// This method is safe to call without holding the CodeCache_lock, as long as a dead codeblob is not
0N/A// looked up (i.e., one that has been marked for deletion). It only dependes on the _segmap to contain
0N/A// valid indices, which it will always do, as long as the CodeBlob is not in the process of being recycled.
0N/ACodeBlob* CodeCache::find_blob(void* start) {
0N/A CodeBlob* result = find_blob_unsafe(start);
0N/A if (result == NULL) return NULL;
0N/A // We could potientially look up non_entrant methods
0N/A guarantee(!result->is_zombie() || result->is_locked_by_vm() || is_error_reported(), "unsafe access to zombie method");
0N/A return result;
0N/A}
0N/A
0N/Anmethod* CodeCache::find_nmethod(void* start) {
0N/A CodeBlob *cb = find_blob(start);
0N/A assert(cb == NULL || cb->is_nmethod(), "did not find an nmethod");
0N/A return (nmethod*)cb;
0N/A}
0N/A
0N/A
0N/Avoid CodeCache::blobs_do(void f(CodeBlob* nm)) {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A FOR_ALL_BLOBS(p) {
0N/A f(p);
0N/A }
0N/A}
0N/A
0N/A
0N/Avoid CodeCache::nmethods_do(void f(nmethod* nm)) {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A FOR_ALL_BLOBS(nm) {
0N/A if (nm->is_nmethod()) f((nmethod*)nm);
0N/A }
0N/A}
0N/A
0N/A
0N/Aint CodeCache::alignment_unit() {
0N/A return (int)_heap->alignment_unit();
0N/A}
0N/A
0N/A
0N/Aint CodeCache::alignment_offset() {
0N/A return (int)_heap->alignment_offset();
0N/A}
0N/A
0N/A
1483N/A// Mark nmethods for unloading if they contain otherwise unreachable
1483N/A// oops.
0N/Avoid CodeCache::do_unloading(BoolObjectClosure* is_alive,
0N/A OopClosure* keep_alive,
0N/A bool unloading_occurred) {
0N/A assert_locked_or_safepoint(CodeCache_lock);
1483N/A FOR_ALL_ALIVE_NMETHODS(nm) {
1483N/A nm->do_unloading(is_alive, keep_alive, unloading_occurred);
0N/A }
0N/A}
0N/A
989N/Avoid CodeCache::blobs_do(CodeBlobClosure* f) {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A FOR_ALL_ALIVE_BLOBS(cb) {
989N/A f->do_code_blob(cb);
989N/A
989N/A#ifdef ASSERT
989N/A if (cb->is_nmethod())
989N/A ((nmethod*)cb)->verify_scavenge_root_oops();
989N/A#endif //ASSERT
0N/A }
0N/A}
0N/A
989N/A// Walk the list of methods which might contain non-perm oops.
989N/Avoid CodeCache::scavenge_root_nmethods_do(CodeBlobClosure* f) {
989N/A assert_locked_or_safepoint(CodeCache_lock);
989N/A debug_only(mark_scavenge_root_nmethods());
989N/A
989N/A for (nmethod* cur = scavenge_root_nmethods(); cur != NULL; cur = cur->scavenge_root_link()) {
989N/A debug_only(cur->clear_scavenge_root_marked());
989N/A assert(cur->scavenge_root_not_marked(), "");
989N/A assert(cur->on_scavenge_root_list(), "else shouldn't be on this list");
989N/A
989N/A bool is_live = (!cur->is_zombie() && !cur->is_unloaded());
989N/A#ifndef PRODUCT
989N/A if (TraceScavenge) {
989N/A cur->print_on(tty, is_live ? "scavenge root" : "dead scavenge root"); tty->cr();
989N/A }
989N/A#endif //PRODUCT
1363N/A if (is_live) {
989N/A // Perform cur->oops_do(f), maybe just once per nmethod.
989N/A f->do_code_blob(cur);
1363N/A cur->fix_oop_relocations();
1363N/A }
989N/A }
989N/A
989N/A // Check for stray marks.
989N/A debug_only(verify_perm_nmethods(NULL));
989N/A}
989N/A
989N/Avoid CodeCache::add_scavenge_root_nmethod(nmethod* nm) {
989N/A assert_locked_or_safepoint(CodeCache_lock);
989N/A nm->set_on_scavenge_root_list();
989N/A nm->set_scavenge_root_link(_scavenge_root_nmethods);
989N/A set_scavenge_root_nmethods(nm);
989N/A print_trace("add_scavenge_root", nm);
989N/A}
989N/A
989N/Avoid CodeCache::drop_scavenge_root_nmethod(nmethod* nm) {
989N/A assert_locked_or_safepoint(CodeCache_lock);
989N/A print_trace("drop_scavenge_root", nm);
989N/A nmethod* last = NULL;
989N/A nmethod* cur = scavenge_root_nmethods();
989N/A while (cur != NULL) {
989N/A nmethod* next = cur->scavenge_root_link();
989N/A if (cur == nm) {
989N/A if (last != NULL)
989N/A last->set_scavenge_root_link(next);
989N/A else set_scavenge_root_nmethods(next);
989N/A nm->set_scavenge_root_link(NULL);
989N/A nm->clear_on_scavenge_root_list();
989N/A return;
989N/A }
989N/A last = cur;
989N/A cur = next;
989N/A }
989N/A assert(false, "should have been on list");
989N/A}
989N/A
989N/Avoid CodeCache::prune_scavenge_root_nmethods() {
989N/A assert_locked_or_safepoint(CodeCache_lock);
989N/A debug_only(mark_scavenge_root_nmethods());
989N/A
989N/A nmethod* last = NULL;
989N/A nmethod* cur = scavenge_root_nmethods();
989N/A while (cur != NULL) {
989N/A nmethod* next = cur->scavenge_root_link();
989N/A debug_only(cur->clear_scavenge_root_marked());
989N/A assert(cur->scavenge_root_not_marked(), "");
989N/A assert(cur->on_scavenge_root_list(), "else shouldn't be on this list");
989N/A
989N/A if (!cur->is_zombie() && !cur->is_unloaded()
989N/A && cur->detect_scavenge_root_oops()) {
989N/A // Keep it. Advance 'last' to prevent deletion.
989N/A last = cur;
989N/A } else {
989N/A // Prune it from the list, so we don't have to look at it any more.
989N/A print_trace("prune_scavenge_root", cur);
989N/A cur->set_scavenge_root_link(NULL);
989N/A cur->clear_on_scavenge_root_list();
989N/A if (last != NULL)
989N/A last->set_scavenge_root_link(next);
989N/A else set_scavenge_root_nmethods(next);
989N/A }
989N/A cur = next;
989N/A }
989N/A
989N/A // Check for stray marks.
989N/A debug_only(verify_perm_nmethods(NULL));
989N/A}
989N/A
989N/A#ifndef PRODUCT
989N/Avoid CodeCache::asserted_non_scavengable_nmethods_do(CodeBlobClosure* f) {
989N/A // While we are here, verify the integrity of the list.
989N/A mark_scavenge_root_nmethods();
989N/A for (nmethod* cur = scavenge_root_nmethods(); cur != NULL; cur = cur->scavenge_root_link()) {
989N/A assert(cur->on_scavenge_root_list(), "else shouldn't be on this list");
989N/A cur->clear_scavenge_root_marked();
989N/A }
989N/A verify_perm_nmethods(f);
989N/A}
989N/A
989N/A// Temporarily mark nmethods that are claimed to be on the non-perm list.
989N/Avoid CodeCache::mark_scavenge_root_nmethods() {
989N/A FOR_ALL_ALIVE_BLOBS(cb) {
989N/A if (cb->is_nmethod()) {
989N/A nmethod *nm = (nmethod*)cb;
989N/A assert(nm->scavenge_root_not_marked(), "clean state");
989N/A if (nm->on_scavenge_root_list())
989N/A nm->set_scavenge_root_marked();
989N/A }
989N/A }
989N/A}
989N/A
989N/A// If the closure is given, run it on the unlisted nmethods.
989N/A// Also make sure that the effects of mark_scavenge_root_nmethods is gone.
989N/Avoid CodeCache::verify_perm_nmethods(CodeBlobClosure* f_or_null) {
989N/A FOR_ALL_ALIVE_BLOBS(cb) {
989N/A bool call_f = (f_or_null != NULL);
989N/A if (cb->is_nmethod()) {
989N/A nmethod *nm = (nmethod*)cb;
989N/A assert(nm->scavenge_root_not_marked(), "must be already processed");
989N/A if (nm->on_scavenge_root_list())
989N/A call_f = false; // don't show this one to the client
989N/A nm->verify_scavenge_root_oops();
989N/A } else {
989N/A call_f = false; // not an nmethod
989N/A }
989N/A if (call_f) f_or_null->do_code_blob(cb);
989N/A }
989N/A}
989N/A#endif //PRODUCT
989N/A
1202N/A
1202N/Anmethod* CodeCache::find_and_remove_saved_code(methodOop m) {
1202N/A MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
1202N/A nmethod* saved = _saved_nmethods;
1202N/A nmethod* prev = NULL;
1202N/A while (saved != NULL) {
1202N/A if (saved->is_in_use() && saved->method() == m) {
1202N/A if (prev != NULL) {
1202N/A prev->set_saved_nmethod_link(saved->saved_nmethod_link());
1202N/A } else {
1202N/A _saved_nmethods = saved->saved_nmethod_link();
1202N/A }
1202N/A assert(saved->is_speculatively_disconnected(), "shouldn't call for other nmethods");
1202N/A saved->set_speculatively_disconnected(false);
1202N/A saved->set_saved_nmethod_link(NULL);
1202N/A if (PrintMethodFlushing) {
1458N/A saved->print_on(tty, " ### nmethod is reconnected\n");
1202N/A }
1202N/A if (LogCompilation && (xtty != NULL)) {
1202N/A ttyLocker ttyl;
1202N/A xtty->begin_elem("nmethod_reconnected compile_id='%3d'", saved->compile_id());
1202N/A xtty->method(methodOop(m));
1202N/A xtty->stamp();
1202N/A xtty->end_elem();
1202N/A }
1202N/A return saved;
1202N/A }
1202N/A prev = saved;
1202N/A saved = saved->saved_nmethod_link();
1202N/A }
1202N/A return NULL;
1202N/A}
1202N/A
1202N/Avoid CodeCache::remove_saved_code(nmethod* nm) {
1458N/A // For conc swpr this will be called with CodeCache_lock taken by caller
1458N/A assert_locked_or_safepoint(CodeCache_lock);
1202N/A assert(nm->is_speculatively_disconnected(), "shouldn't call for other nmethods");
1202N/A nmethod* saved = _saved_nmethods;
1202N/A nmethod* prev = NULL;
1202N/A while (saved != NULL) {
1202N/A if (saved == nm) {
1202N/A if (prev != NULL) {
1202N/A prev->set_saved_nmethod_link(saved->saved_nmethod_link());
1202N/A } else {
1202N/A _saved_nmethods = saved->saved_nmethod_link();
1202N/A }
1202N/A if (LogCompilation && (xtty != NULL)) {
1202N/A ttyLocker ttyl;
1202N/A xtty->begin_elem("nmethod_removed compile_id='%3d'", nm->compile_id());
1202N/A xtty->stamp();
1202N/A xtty->end_elem();
1202N/A }
1202N/A return;
1202N/A }
1202N/A prev = saved;
1202N/A saved = saved->saved_nmethod_link();
1202N/A }
1202N/A ShouldNotReachHere();
1202N/A}
1202N/A
1202N/Avoid CodeCache::speculatively_disconnect(nmethod* nm) {
1202N/A assert_locked_or_safepoint(CodeCache_lock);
1202N/A assert(nm->is_in_use() && !nm->is_speculatively_disconnected(), "should only disconnect live nmethods");
1202N/A nm->set_saved_nmethod_link(_saved_nmethods);
1202N/A _saved_nmethods = nm;
1202N/A if (PrintMethodFlushing) {
1458N/A nm->print_on(tty, " ### nmethod is speculatively disconnected\n");
1202N/A }
1202N/A if (LogCompilation && (xtty != NULL)) {
1202N/A ttyLocker ttyl;
1202N/A xtty->begin_elem("nmethod_disconnected compile_id='%3d'", nm->compile_id());
1202N/A xtty->method(methodOop(nm->method()));
1202N/A xtty->stamp();
1202N/A xtty->end_elem();
1202N/A }
1202N/A nm->method()->clear_code();
1202N/A nm->set_speculatively_disconnected(true);
1202N/A}
1202N/A
1202N/A
0N/Avoid CodeCache::gc_prologue() {
989N/A assert(!nmethod::oops_do_marking_is_active(), "oops_do_marking_epilogue must be called");
0N/A}
0N/A
0N/A
0N/Avoid CodeCache::gc_epilogue() {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A FOR_ALL_ALIVE_BLOBS(cb) {
0N/A if (cb->is_nmethod()) {
0N/A nmethod *nm = (nmethod*)cb;
0N/A assert(!nm->is_unloaded(), "Tautology");
0N/A if (needs_cache_clean()) {
0N/A nm->cleanup_inline_caches();
0N/A }
1483N/A DEBUG_ONLY(nm->verify());
1483N/A nm->fix_oop_relocations();
0N/A }
0N/A }
0N/A set_needs_cache_clean(false);
989N/A prune_scavenge_root_nmethods();
989N/A assert(!nmethod::oops_do_marking_is_active(), "oops_do_marking_prologue must be called");
0N/A}
0N/A
0N/A
0N/Aaddress CodeCache::first_address() {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A return (address)_heap->begin();
0N/A}
0N/A
0N/A
0N/Aaddress CodeCache::last_address() {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A return (address)_heap->end();
0N/A}
0N/A
0N/A
0N/Avoid icache_init();
0N/A
0N/Avoid CodeCache::initialize() {
0N/A assert(CodeCacheSegmentSize >= (uintx)CodeEntryAlignment, "CodeCacheSegmentSize must be large enough to align entry points");
0N/A#ifdef COMPILER2
0N/A assert(CodeCacheSegmentSize >= (uintx)OptoLoopAlignment, "CodeCacheSegmentSize must be large enough to align inner loops");
0N/A#endif
0N/A assert(CodeCacheSegmentSize >= sizeof(jdouble), "CodeCacheSegmentSize must be large enough to align constants");
0N/A // This was originally just a check of the alignment, causing failure, instead, round
0N/A // the code cache to the page size. In particular, Solaris is moving to a larger
0N/A // default page size.
0N/A CodeCacheExpansionSize = round_to(CodeCacheExpansionSize, os::vm_page_size());
0N/A InitialCodeCacheSize = round_to(InitialCodeCacheSize, os::vm_page_size());
0N/A ReservedCodeCacheSize = round_to(ReservedCodeCacheSize, os::vm_page_size());
0N/A if (!_heap->reserve(ReservedCodeCacheSize, InitialCodeCacheSize, CodeCacheSegmentSize)) {
0N/A vm_exit_during_initialization("Could not reserve enough space for code cache");
0N/A }
0N/A
0N/A MemoryService::add_code_heap_memory_pool(_heap);
0N/A
0N/A // Initialize ICache flush mechanism
0N/A // This service is needed for os::register_code_area
0N/A icache_init();
0N/A
0N/A // Give OS a chance to register generated code area.
0N/A // This is used on Windows 64 bit platforms to register
0N/A // Structured Exception Handlers for our generated code.
0N/A os::register_code_area(_heap->low_boundary(), _heap->high_boundary());
0N/A}
0N/A
0N/A
0N/Avoid codeCache_init() {
0N/A CodeCache::initialize();
0N/A}
0N/A
0N/A//------------------------------------------------------------------------------------------------
0N/A
0N/Aint CodeCache::number_of_nmethods_with_dependencies() {
0N/A return _number_of_nmethods_with_dependencies;
0N/A}
0N/A
0N/Avoid CodeCache::clear_inline_caches() {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A FOR_ALL_ALIVE_NMETHODS(nm) {
0N/A nm->clear_inline_caches();
0N/A }
0N/A}
0N/A
0N/A#ifndef PRODUCT
0N/A// used to keep track of how much time is spent in mark_for_deoptimization
0N/Astatic elapsedTimer dependentCheckTime;
0N/Astatic int dependentCheckCount = 0;
0N/A#endif // PRODUCT
0N/A
0N/A
0N/Aint CodeCache::mark_for_deoptimization(DepChange& changes) {
0N/A MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
0N/A
0N/A#ifndef PRODUCT
0N/A dependentCheckTime.start();
0N/A dependentCheckCount++;
0N/A#endif // PRODUCT
0N/A
0N/A int number_of_marked_CodeBlobs = 0;
0N/A
0N/A // search the hierarchy looking for nmethods which are affected by the loading of this class
0N/A
0N/A // then search the interfaces this class implements looking for nmethods
0N/A // which might be dependent of the fact that an interface only had one
0N/A // implementor.
0N/A
0N/A { No_Safepoint_Verifier nsv;
0N/A for (DepChange::ContextStream str(changes, nsv); str.next(); ) {
0N/A klassOop d = str.klass();
0N/A number_of_marked_CodeBlobs += instanceKlass::cast(d)->mark_dependent_nmethods(changes);
0N/A }
0N/A }
0N/A
0N/A if (VerifyDependencies) {
0N/A // Turn off dependency tracing while actually testing deps.
0N/A NOT_PRODUCT( FlagSetting fs(TraceDependencies, false) );
0N/A FOR_ALL_ALIVE_NMETHODS(nm) {
0N/A if (!nm->is_marked_for_deoptimization() &&
0N/A nm->check_all_dependencies()) {
0N/A ResourceMark rm;
0N/A tty->print_cr("Should have been marked for deoptimization:");
0N/A changes.print();
0N/A nm->print();
0N/A nm->print_dependencies();
0N/A }
0N/A }
0N/A }
0N/A
0N/A#ifndef PRODUCT
0N/A dependentCheckTime.stop();
0N/A#endif // PRODUCT
0N/A
0N/A return number_of_marked_CodeBlobs;
0N/A}
0N/A
0N/A
0N/A#ifdef HOTSWAP
0N/Aint CodeCache::mark_for_evol_deoptimization(instanceKlassHandle dependee) {
0N/A MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
0N/A int number_of_marked_CodeBlobs = 0;
0N/A
0N/A // Deoptimize all methods of the evolving class itself
0N/A objArrayOop old_methods = dependee->methods();
0N/A for (int i = 0; i < old_methods->length(); i++) {
0N/A ResourceMark rm;
0N/A methodOop old_method = (methodOop) old_methods->obj_at(i);
0N/A nmethod *nm = old_method->code();
0N/A if (nm != NULL) {
0N/A nm->mark_for_deoptimization();
0N/A number_of_marked_CodeBlobs++;
0N/A }
0N/A }
0N/A
0N/A FOR_ALL_ALIVE_NMETHODS(nm) {
0N/A if (nm->is_marked_for_deoptimization()) {
0N/A // ...Already marked in the previous pass; don't count it again.
0N/A } else if (nm->is_evol_dependent_on(dependee())) {
0N/A ResourceMark rm;
0N/A nm->mark_for_deoptimization();
0N/A number_of_marked_CodeBlobs++;
0N/A } else {
0N/A // flush caches in case they refer to a redefined methodOop
0N/A nm->clear_inline_caches();
0N/A }
0N/A }
0N/A
0N/A return number_of_marked_CodeBlobs;
0N/A}
0N/A#endif // HOTSWAP
0N/A
0N/A
0N/A// Deoptimize all methods
0N/Avoid CodeCache::mark_all_nmethods_for_deoptimization() {
0N/A MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
0N/A FOR_ALL_ALIVE_NMETHODS(nm) {
0N/A nm->mark_for_deoptimization();
0N/A }
0N/A}
0N/A
0N/A
0N/Aint CodeCache::mark_for_deoptimization(methodOop dependee) {
0N/A MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
0N/A int number_of_marked_CodeBlobs = 0;
0N/A
0N/A FOR_ALL_ALIVE_NMETHODS(nm) {
0N/A if (nm->is_dependent_on_method(dependee)) {
0N/A ResourceMark rm;
0N/A nm->mark_for_deoptimization();
0N/A number_of_marked_CodeBlobs++;
0N/A }
0N/A }
0N/A
0N/A return number_of_marked_CodeBlobs;
0N/A}
0N/A
0N/Avoid CodeCache::make_marked_nmethods_zombies() {
0N/A assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
0N/A FOR_ALL_ALIVE_NMETHODS(nm) {
0N/A if (nm->is_marked_for_deoptimization()) {
0N/A
0N/A // If the nmethod has already been made non-entrant and it can be converted
0N/A // then zombie it now. Otherwise make it non-entrant and it will eventually
0N/A // be zombied when it is no longer seen on the stack. Note that the nmethod
0N/A // might be "entrant" and not on the stack and so could be zombied immediately
0N/A // but we can't tell because we don't track it on stack until it becomes
0N/A // non-entrant.
0N/A
0N/A if (nm->is_not_entrant() && nm->can_not_entrant_be_converted()) {
0N/A nm->make_zombie();
0N/A } else {
0N/A nm->make_not_entrant();
0N/A }
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid CodeCache::make_marked_nmethods_not_entrant() {
0N/A assert_locked_or_safepoint(CodeCache_lock);
0N/A FOR_ALL_ALIVE_NMETHODS(nm) {
0N/A if (nm->is_marked_for_deoptimization()) {
0N/A nm->make_not_entrant();
0N/A }
0N/A }
0N/A}
0N/A
0N/Avoid CodeCache::verify() {
0N/A _heap->verify();
0N/A FOR_ALL_ALIVE_BLOBS(p) {
0N/A p->verify();
0N/A }
0N/A}
0N/A
0N/A//------------------------------------------------------------------------------------------------
0N/A// Non-product version
0N/A
0N/A#ifndef PRODUCT
0N/A
0N/Avoid CodeCache::verify_if_often() {
0N/A if (VerifyCodeCacheOften) {
0N/A _heap->verify();
0N/A }
0N/A}
0N/A
989N/Avoid CodeCache::print_trace(const char* event, CodeBlob* cb, int size) {
989N/A if (PrintCodeCache2) { // Need to add a new flag
989N/A ResourceMark rm;
989N/A if (size == 0) size = cb->size();
989N/A tty->print_cr("CodeCache %s: addr: " INTPTR_FORMAT ", size: 0x%x", event, cb, size);
989N/A }
989N/A}
989N/A
0N/Avoid CodeCache::print_internals() {
0N/A int nmethodCount = 0;
0N/A int runtimeStubCount = 0;
0N/A int adapterCount = 0;
0N/A int deoptimizationStubCount = 0;
0N/A int uncommonTrapStubCount = 0;
0N/A int bufferBlobCount = 0;
0N/A int total = 0;
0N/A int nmethodAlive = 0;
0N/A int nmethodNotEntrant = 0;
0N/A int nmethodZombie = 0;
0N/A int nmethodUnloaded = 0;
0N/A int nmethodJava = 0;
0N/A int nmethodNative = 0;
0N/A int maxCodeSize = 0;
0N/A ResourceMark rm;
0N/A
0N/A CodeBlob *cb;
0N/A for (cb = first(); cb != NULL; cb = next(cb)) {
0N/A total++;
0N/A if (cb->is_nmethod()) {
0N/A nmethod* nm = (nmethod*)cb;
0N/A
0N/A if (Verbose && nm->method() != NULL) {
0N/A ResourceMark rm;
0N/A char *method_name = nm->method()->name_and_sig_as_C_string();
0N/A tty->print("%s", method_name);
0N/A if(nm->is_alive()) { tty->print_cr(" alive"); }
0N/A if(nm->is_not_entrant()) { tty->print_cr(" not-entrant"); }
0N/A if(nm->is_zombie()) { tty->print_cr(" zombie"); }
0N/A }
0N/A
0N/A nmethodCount++;
0N/A
0N/A if(nm->is_alive()) { nmethodAlive++; }
0N/A if(nm->is_not_entrant()) { nmethodNotEntrant++; }
0N/A if(nm->is_zombie()) { nmethodZombie++; }
0N/A if(nm->is_unloaded()) { nmethodUnloaded++; }
0N/A if(nm->is_native_method()) { nmethodNative++; }
0N/A
0N/A if(nm->method() != NULL && nm->is_java_method()) {
0N/A nmethodJava++;
1668N/A if (nm->insts_size() > maxCodeSize) {
1668N/A maxCodeSize = nm->insts_size();
0N/A }
0N/A }
0N/A } else if (cb->is_runtime_stub()) {
0N/A runtimeStubCount++;
0N/A } else if (cb->is_deoptimization_stub()) {
0N/A deoptimizationStubCount++;
0N/A } else if (cb->is_uncommon_trap_stub()) {
0N/A uncommonTrapStubCount++;
0N/A } else if (cb->is_adapter_blob()) {
0N/A adapterCount++;
0N/A } else if (cb->is_buffer_blob()) {
0N/A bufferBlobCount++;
0N/A }
0N/A }
0N/A
0N/A int bucketSize = 512;
0N/A int bucketLimit = maxCodeSize / bucketSize + 1;
0N/A int *buckets = NEW_C_HEAP_ARRAY(int, bucketLimit);
0N/A memset(buckets,0,sizeof(int) * bucketLimit);
0N/A
0N/A for (cb = first(); cb != NULL; cb = next(cb)) {
0N/A if (cb->is_nmethod()) {
0N/A nmethod* nm = (nmethod*)cb;
0N/A if(nm->is_java_method()) {
1668N/A buckets[nm->insts_size() / bucketSize]++;
0N/A }
0N/A }
0N/A }
0N/A tty->print_cr("Code Cache Entries (total of %d)",total);
0N/A tty->print_cr("-------------------------------------------------");
0N/A tty->print_cr("nmethods: %d",nmethodCount);
0N/A tty->print_cr("\talive: %d",nmethodAlive);
0N/A tty->print_cr("\tnot_entrant: %d",nmethodNotEntrant);
0N/A tty->print_cr("\tzombie: %d",nmethodZombie);
0N/A tty->print_cr("\tunloaded: %d",nmethodUnloaded);
0N/A tty->print_cr("\tjava: %d",nmethodJava);
0N/A tty->print_cr("\tnative: %d",nmethodNative);
0N/A tty->print_cr("runtime_stubs: %d",runtimeStubCount);
0N/A tty->print_cr("adapters: %d",adapterCount);
0N/A tty->print_cr("buffer blobs: %d",bufferBlobCount);
0N/A tty->print_cr("deoptimization_stubs: %d",deoptimizationStubCount);
0N/A tty->print_cr("uncommon_traps: %d",uncommonTrapStubCount);
0N/A tty->print_cr("\nnmethod size distribution (non-zombie java)");
0N/A tty->print_cr("-------------------------------------------------");
0N/A
0N/A for(int i=0; i<bucketLimit; i++) {
0N/A if(buckets[i] != 0) {
0N/A tty->print("%d - %d bytes",i*bucketSize,(i+1)*bucketSize);
0N/A tty->fill_to(40);
0N/A tty->print_cr("%d",buckets[i]);
0N/A }
0N/A }
0N/A
0N/A FREE_C_HEAP_ARRAY(int, buckets);
0N/A}
0N/A
0N/Avoid CodeCache::print() {
0N/A CodeBlob_sizes live;
0N/A CodeBlob_sizes dead;
0N/A
0N/A FOR_ALL_BLOBS(p) {
0N/A if (!p->is_alive()) {
0N/A dead.add(p);
0N/A } else {
0N/A live.add(p);
0N/A }
0N/A }
0N/A
0N/A tty->print_cr("CodeCache:");
0N/A
0N/A tty->print_cr("nmethod dependency checking time %f", dependentCheckTime.seconds(),
0N/A dependentCheckTime.seconds() / dependentCheckCount);
0N/A
0N/A if (!live.is_empty()) {
0N/A live.print("live");
0N/A }
0N/A if (!dead.is_empty()) {
0N/A dead.print("dead");
0N/A }
0N/A
0N/A
0N/A if (Verbose) {
0N/A // print the oop_map usage
0N/A int code_size = 0;
0N/A int number_of_blobs = 0;
0N/A int number_of_oop_maps = 0;
0N/A int map_size = 0;
0N/A FOR_ALL_BLOBS(p) {
0N/A if (p->is_alive()) {
0N/A number_of_blobs++;
1668N/A code_size += p->code_size();
0N/A OopMapSet* set = p->oop_maps();
0N/A if (set != NULL) {
0N/A number_of_oop_maps += set->size();
1668N/A map_size += set->heap_size();
0N/A }
0N/A }
0N/A }
0N/A tty->print_cr("OopMaps");
0N/A tty->print_cr(" #blobs = %d", number_of_blobs);
0N/A tty->print_cr(" code size = %d", code_size);
0N/A tty->print_cr(" #oop_maps = %d", number_of_oop_maps);
0N/A tty->print_cr(" map size = %d", map_size);
0N/A }
0N/A
0N/A}
0N/A
0N/A#endif // PRODUCT
1827N/A
1827N/Avoid CodeCache::print_bounds(outputStream* st) {
1827N/A st->print_cr("Code Cache [" INTPTR_FORMAT ", " INTPTR_FORMAT ", " INTPTR_FORMAT ")",
1827N/A _heap->low_boundary(),
1827N/A _heap->high(),
1827N/A _heap->high_boundary());
1827N/A st->print_cr(" total_blobs=" UINT32_FORMAT " nmethods=" UINT32_FORMAT
2200N/A " adapters=" UINT32_FORMAT " free_code_cache=" SIZE_FORMAT "Kb"
1979N/A " largest_free_block=" SIZE_FORMAT,
2200N/A nof_blobs(), nof_nmethods(), nof_adapters(),
2200N/A unallocated_capacity()/K, largest_free_block());
1827N/A}
2200N/A
2200N/Avoid CodeCache::log_state(outputStream* st) {
2200N/A st->print(" total_blobs='" UINT32_FORMAT "' nmethods='" UINT32_FORMAT "'"
2200N/A " adapters='" UINT32_FORMAT "' free_code_cache='" SIZE_FORMAT "'"
2200N/A " largest_free_block='" SIZE_FORMAT "'",
2200N/A nof_blobs(), nof_nmethods(), nof_adapters(),
2200N/A unallocated_capacity(), largest_free_block());
2200N/A}