aoqi@0: /* aoqi@0: * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "code/codeBlob.hpp" aoqi@0: #include "code/codeCache.hpp" aoqi@0: #include "code/scopeDesc.hpp" aoqi@0: #include "code/vtableStubs.hpp" aoqi@0: #include "memory/resourceArea.hpp" aoqi@0: #include "oops/oop.inline.hpp" aoqi@0: #include "prims/jvmtiCodeBlobEvents.hpp" aoqi@0: #include "prims/jvmtiExport.hpp" aoqi@0: #include "runtime/handles.hpp" aoqi@0: #include "runtime/handles.inline.hpp" aoqi@0: #include "runtime/vmThread.hpp" aoqi@0: aoqi@0: // Support class to collect a list of the non-nmethod CodeBlobs in aoqi@0: // the CodeCache. aoqi@0: // aoqi@0: // This class actually creates a list of JvmtiCodeBlobDesc - each JvmtiCodeBlobDesc aoqi@0: // describes a single CodeBlob in the CodeCache. Note that collection is aoqi@0: // done to a static list - this is because CodeCache::blobs_do is defined aoqi@0: // as void CodeCache::blobs_do(void f(CodeBlob* nm)) and hence requires aoqi@0: // a C or static method. aoqi@0: // aoqi@0: // Usage :- aoqi@0: // aoqi@0: // CodeBlobCollector collector; aoqi@0: // aoqi@0: // collector.collect(); aoqi@0: // JvmtiCodeBlobDesc* blob = collector.first(); aoqi@0: // while (blob != NULL) { aoqi@0: // : aoqi@0: // blob = collector.next(); aoqi@0: // } aoqi@0: // aoqi@0: aoqi@0: class CodeBlobCollector : StackObj { aoqi@0: private: aoqi@0: GrowableArray* _code_blobs; // collected blobs aoqi@0: int _pos; // iterator position aoqi@0: aoqi@0: // used during a collection aoqi@0: static GrowableArray* _global_code_blobs; aoqi@0: static void do_blob(CodeBlob* cb); aoqi@0: static void do_vtable_stub(VtableStub* vs); aoqi@0: public: aoqi@0: CodeBlobCollector() { aoqi@0: _code_blobs = NULL; aoqi@0: _pos = -1; aoqi@0: } aoqi@0: ~CodeBlobCollector() { aoqi@0: if (_code_blobs != NULL) { aoqi@0: for (int i=0; i<_code_blobs->length(); i++) { aoqi@0: FreeHeap(_code_blobs->at(i)); aoqi@0: } aoqi@0: delete _code_blobs; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // collect list of code blobs in the cache aoqi@0: void collect(); aoqi@0: aoqi@0: // iteration support - return first code blob aoqi@0: JvmtiCodeBlobDesc* first() { aoqi@0: assert(_code_blobs != NULL, "not collected"); aoqi@0: if (_code_blobs->length() == 0) { aoqi@0: return NULL; aoqi@0: } aoqi@0: _pos = 0; aoqi@0: return _code_blobs->at(0); aoqi@0: } aoqi@0: aoqi@0: // iteration support - return next code blob aoqi@0: JvmtiCodeBlobDesc* next() { aoqi@0: assert(_pos >= 0, "iteration not started"); aoqi@0: if (_pos+1 >= _code_blobs->length()) { aoqi@0: return NULL; aoqi@0: } aoqi@0: return _code_blobs->at(++_pos); aoqi@0: } aoqi@0: aoqi@0: }; aoqi@0: aoqi@0: // used during collection aoqi@0: GrowableArray* CodeBlobCollector::_global_code_blobs; aoqi@0: aoqi@0: aoqi@0: // called for each CodeBlob in the CodeCache aoqi@0: // aoqi@0: // This function filters out nmethods as it is only interested in aoqi@0: // other CodeBlobs. This function also filters out CodeBlobs that have aoqi@0: // a duplicate starting address as previous blobs. This is needed to aoqi@0: // handle the case where multiple stubs are generated into a single aoqi@0: // BufferBlob. aoqi@0: aoqi@0: void CodeBlobCollector::do_blob(CodeBlob* cb) { aoqi@0: aoqi@0: // ignore nmethods aoqi@0: if (cb->is_nmethod()) { aoqi@0: return; aoqi@0: } aoqi@0: // exclude VtableStubs, which are processed separately aoqi@0: if (cb->is_buffer_blob() && strcmp(cb->name(), "vtable chunks") == 0) { aoqi@0: return; aoqi@0: } aoqi@0: aoqi@0: // check if this starting address has been seen already - the aoqi@0: // assumption is that stubs are inserted into the list before the aoqi@0: // enclosing BufferBlobs. aoqi@0: address addr = cb->code_begin(); aoqi@0: for (int i=0; i<_global_code_blobs->length(); i++) { aoqi@0: JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i); aoqi@0: if (addr == scb->code_begin()) { aoqi@0: return; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: // record the CodeBlob details as a JvmtiCodeBlobDesc aoqi@0: JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(cb->name(), cb->code_begin(), cb->code_end()); aoqi@0: _global_code_blobs->append(scb); aoqi@0: } aoqi@0: aoqi@0: // called for each VtableStub in VtableStubs aoqi@0: aoqi@0: void CodeBlobCollector::do_vtable_stub(VtableStub* vs) { aoqi@0: JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(vs->is_vtable_stub() ? "vtable stub" : "itable stub", aoqi@0: vs->code_begin(), vs->code_end()); aoqi@0: _global_code_blobs->append(scb); aoqi@0: } aoqi@0: aoqi@0: // collects a list of CodeBlobs in the CodeCache. aoqi@0: // aoqi@0: // The created list is growable array of JvmtiCodeBlobDesc - each one describes aoqi@0: // a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do aoqi@0: // requires a a C or static function so we can't use an instance function. This aoqi@0: // isn't a problem as the iteration is serial anyway as we need the CodeCache_lock aoqi@0: // to iterate over the code cache. aoqi@0: // aoqi@0: // Note that the CodeBlobs in the CodeCache will include BufferBlobs that may aoqi@0: // contain multiple stubs. As a profiler is interested in the stubs rather than aoqi@0: // the enclosing container we first iterate over the stub code descriptors so aoqi@0: // that the stubs go into the list first. do_blob will then filter out the aoqi@0: // enclosing blobs if the starting address of the enclosing blobs matches the aoqi@0: // starting address of first stub generated in the enclosing blob. aoqi@0: aoqi@0: void CodeBlobCollector::collect() { aoqi@0: assert_locked_or_safepoint(CodeCache_lock); aoqi@0: assert(_global_code_blobs == NULL, "checking"); aoqi@0: aoqi@0: // create the global list aoqi@0: _global_code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray(50,true); aoqi@0: aoqi@0: // iterate over the stub code descriptors and put them in the list first. aoqi@0: int index = 0; aoqi@0: StubCodeDesc* desc; aoqi@0: while ((desc = StubCodeDesc::desc_for_index(++index)) != NULL) { aoqi@0: _global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end())); aoqi@0: } aoqi@0: aoqi@0: // Vtable stubs are not described with StubCodeDesc, aoqi@0: // process them separately aoqi@0: VtableStubs::vtable_stub_do(do_vtable_stub); aoqi@0: aoqi@0: // next iterate over all the non-nmethod code blobs and add them to aoqi@0: // the list - as noted above this will filter out duplicates and aoqi@0: // enclosing blobs. aoqi@0: CodeCache::blobs_do(do_blob); aoqi@0: aoqi@0: // make the global list the instance list so that it can be used aoqi@0: // for other iterations. aoqi@0: _code_blobs = _global_code_blobs; aoqi@0: _global_code_blobs = NULL; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob. aoqi@0: aoqi@0: jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) { aoqi@0: CodeBlobCollector collector; aoqi@0: aoqi@0: // First collect all the code blobs. This has to be done in a aoqi@0: // single pass over the code cache with CodeCache_lock held because aoqi@0: // there isn't any safe way to iterate over regular CodeBlobs since aoqi@0: // they can be freed at any point. aoqi@0: { aoqi@0: MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: collector.collect(); aoqi@0: } aoqi@0: aoqi@0: // iterate over the collected list and post an event for each blob aoqi@0: JvmtiCodeBlobDesc* blob = collector.first(); aoqi@0: while (blob != NULL) { aoqi@0: JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end()); aoqi@0: blob = collector.next(); aoqi@0: } aoqi@0: return JVMTI_ERROR_NONE; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // Generate a COMPILED_METHOD_LOAD event for each nnmethod aoqi@0: jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) { aoqi@0: HandleMark hm; aoqi@0: aoqi@0: // Walk the CodeCache notifying for live nmethods. The code cache aoqi@0: // may be changing while this is happening which is ok since newly aoqi@0: // created nmethod will notify normally and nmethods which are freed aoqi@0: // can be safely skipped. aoqi@0: MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: nmethod* current = CodeCache::first_nmethod(); aoqi@0: while (current != NULL) { aoqi@0: // Only notify for live nmethods aoqi@0: if (current->is_alive()) { aoqi@0: // Lock the nmethod so it can't be freed aoqi@0: nmethodLocker nml(current); aoqi@0: aoqi@0: // Don't hold the lock over the notify or jmethodID creation aoqi@0: MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); aoqi@0: current->get_and_cache_jmethod_id(); aoqi@0: JvmtiExport::post_compiled_method_load(current); aoqi@0: } aoqi@0: current = CodeCache::next_nmethod(current); aoqi@0: } aoqi@0: return JVMTI_ERROR_NONE; aoqi@0: } aoqi@0: aoqi@0: aoqi@0: // create a C-heap allocated address location map for an nmethod aoqi@0: void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm, aoqi@0: jvmtiAddrLocationMap** map_ptr, aoqi@0: jint *map_length_ptr) aoqi@0: { aoqi@0: ResourceMark rm; aoqi@0: jvmtiAddrLocationMap* map = NULL; aoqi@0: jint map_length = 0; aoqi@0: aoqi@0: aoqi@0: // Generate line numbers using PcDesc and ScopeDesc info aoqi@0: methodHandle mh(nm->method()); aoqi@0: aoqi@0: if (!mh->is_native()) { aoqi@0: PcDesc *pcd; aoqi@0: int pcds_in_method; aoqi@0: aoqi@0: pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin()); aoqi@0: map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method, mtInternal); aoqi@0: aoqi@0: address scopes_data = nm->scopes_data_begin(); aoqi@0: for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) { aoqi@0: ScopeDesc sc0(nm, pcd->scope_decode_offset(), pcd->should_reexecute(), pcd->return_oop()); aoqi@0: ScopeDesc *sd = &sc0; aoqi@0: while( !sd->is_top() ) { sd = sd->sender(); } aoqi@0: int bci = sd->bci(); aoqi@0: if (bci != InvocationEntryBci) { aoqi@0: assert(map_length < pcds_in_method, "checking"); aoqi@0: map[map_length].start_address = (const void*)pcd->real_pc(nm); aoqi@0: map[map_length].location = bci; aoqi@0: ++map_length; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: *map_ptr = map; aoqi@0: *map_length_ptr = map_length; aoqi@0: }