Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/prims/jvmtiCodeBlobEvents.cpp
32285 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "code/codeBlob.hpp"26#include "code/codeCache.hpp"27#include "code/scopeDesc.hpp"28#include "code/vtableStubs.hpp"29#include "memory/resourceArea.hpp"30#include "oops/oop.inline.hpp"31#include "prims/jvmtiCodeBlobEvents.hpp"32#include "prims/jvmtiExport.hpp"33#include "runtime/handles.hpp"34#include "runtime/handles.inline.hpp"35#include "runtime/vmThread.hpp"3637// Support class to collect a list of the non-nmethod CodeBlobs in38// the CodeCache.39//40// This class actually creates a list of JvmtiCodeBlobDesc - each JvmtiCodeBlobDesc41// describes a single CodeBlob in the CodeCache. Note that collection is42// done to a static list - this is because CodeCache::blobs_do is defined43// as void CodeCache::blobs_do(void f(CodeBlob* nm)) and hence requires44// a C or static method.45//46// Usage :-47//48// CodeBlobCollector collector;49//50// collector.collect();51// JvmtiCodeBlobDesc* blob = collector.first();52// while (blob != NULL) {53// :54// blob = collector.next();55// }56//5758class CodeBlobCollector : StackObj {59private:60GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs; // collected blobs61int _pos; // iterator position6263// used during a collection64static GrowableArray<JvmtiCodeBlobDesc*>* _global_code_blobs;65static void do_blob(CodeBlob* cb);66static void do_vtable_stub(VtableStub* vs);67public:68CodeBlobCollector() {69_code_blobs = NULL;70_pos = -1;71}72~CodeBlobCollector() {73if (_code_blobs != NULL) {74for (int i=0; i<_code_blobs->length(); i++) {75FreeHeap(_code_blobs->at(i));76}77delete _code_blobs;78}79}8081// collect list of code blobs in the cache82void collect();8384// iteration support - return first code blob85JvmtiCodeBlobDesc* first() {86assert(_code_blobs != NULL, "not collected");87if (_code_blobs->length() == 0) {88return NULL;89}90_pos = 0;91return _code_blobs->at(0);92}9394// iteration support - return next code blob95JvmtiCodeBlobDesc* next() {96assert(_pos >= 0, "iteration not started");97if (_pos+1 >= _code_blobs->length()) {98return NULL;99}100return _code_blobs->at(++_pos);101}102103};104105// used during collection106GrowableArray<JvmtiCodeBlobDesc*>* CodeBlobCollector::_global_code_blobs;107108109// called for each CodeBlob in the CodeCache110//111// This function filters out nmethods as it is only interested in112// other CodeBlobs. This function also filters out CodeBlobs that have113// a duplicate starting address as previous blobs. This is needed to114// handle the case where multiple stubs are generated into a single115// BufferBlob.116117void CodeBlobCollector::do_blob(CodeBlob* cb) {118119// ignore nmethods120if (cb->is_nmethod()) {121return;122}123// exclude VtableStubs, which are processed separately124if (cb->is_buffer_blob() && strcmp(cb->name(), "vtable chunks") == 0) {125return;126}127128// check if this starting address has been seen already - the129// assumption is that stubs are inserted into the list before the130// enclosing BufferBlobs.131address addr = cb->code_begin();132for (int i=0; i<_global_code_blobs->length(); i++) {133JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i);134if (addr == scb->code_begin()) {135return;136}137}138139// record the CodeBlob details as a JvmtiCodeBlobDesc140JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(cb->name(), cb->code_begin(), cb->code_end());141_global_code_blobs->append(scb);142}143144// called for each VtableStub in VtableStubs145146void CodeBlobCollector::do_vtable_stub(VtableStub* vs) {147JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(vs->is_vtable_stub() ? "vtable stub" : "itable stub",148vs->code_begin(), vs->code_end());149_global_code_blobs->append(scb);150}151152// collects a list of CodeBlobs in the CodeCache.153//154// The created list is growable array of JvmtiCodeBlobDesc - each one describes155// a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do156// requires a a C or static function so we can't use an instance function. This157// isn't a problem as the iteration is serial anyway as we need the CodeCache_lock158// to iterate over the code cache.159//160// Note that the CodeBlobs in the CodeCache will include BufferBlobs that may161// contain multiple stubs. As a profiler is interested in the stubs rather than162// the enclosing container we first iterate over the stub code descriptors so163// that the stubs go into the list first. do_blob will then filter out the164// enclosing blobs if the starting address of the enclosing blobs matches the165// starting address of first stub generated in the enclosing blob.166167void CodeBlobCollector::collect() {168assert_locked_or_safepoint(CodeCache_lock);169assert(_global_code_blobs == NULL, "checking");170171// create the global list172_global_code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(50,true);173174// iterate over the stub code descriptors and put them in the list first.175int index = 0;176StubCodeDesc* desc;177while ((desc = StubCodeDesc::desc_for_index(++index)) != NULL) {178_global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end()));179}180181// Vtable stubs are not described with StubCodeDesc,182// process them separately183VtableStubs::vtable_stub_do(do_vtable_stub);184185// next iterate over all the non-nmethod code blobs and add them to186// the list - as noted above this will filter out duplicates and187// enclosing blobs.188CodeCache::blobs_do(do_blob);189190// make the global list the instance list so that it can be used191// for other iterations.192_code_blobs = _global_code_blobs;193_global_code_blobs = NULL;194}195196197// Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob.198199jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) {200CodeBlobCollector collector;201202// First collect all the code blobs. This has to be done in a203// single pass over the code cache with CodeCache_lock held because204// there isn't any safe way to iterate over regular CodeBlobs since205// they can be freed at any point.206{207MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);208collector.collect();209}210211// iterate over the collected list and post an event for each blob212JvmtiCodeBlobDesc* blob = collector.first();213while (blob != NULL) {214JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end());215blob = collector.next();216}217return JVMTI_ERROR_NONE;218}219220221// Generate a COMPILED_METHOD_LOAD event for each nnmethod222jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) {223HandleMark hm;224225// Walk the CodeCache notifying for live nmethods. The code cache226// may be changing while this is happening which is ok since newly227// created nmethod will notify normally and nmethods which are freed228// can be safely skipped.229MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);230nmethod* current = CodeCache::first_nmethod();231while (current != NULL) {232// Only notify for live nmethods233if (current->is_alive()) {234// Lock the nmethod so it can't be freed235nmethodLocker nml(current);236237// Don't hold the lock over the notify or jmethodID creation238MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);239current->get_and_cache_jmethod_id();240JvmtiExport::post_compiled_method_load(current);241}242current = CodeCache::next_nmethod(current);243}244return JVMTI_ERROR_NONE;245}246247248// create a C-heap allocated address location map for an nmethod249void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm,250jvmtiAddrLocationMap** map_ptr,251jint *map_length_ptr)252{253ResourceMark rm;254jvmtiAddrLocationMap* map = NULL;255jint map_length = 0;256257258// Generate line numbers using PcDesc and ScopeDesc info259methodHandle mh(nm->method());260261if (!mh->is_native()) {262PcDesc *pcd;263int pcds_in_method;264265pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin());266map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method, mtInternal);267268address scopes_data = nm->scopes_data_begin();269for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) {270ScopeDesc sc0(nm, pcd->scope_decode_offset(), pcd->should_reexecute(), pcd->return_oop());271ScopeDesc *sd = &sc0;272while( !sd->is_top() ) { sd = sd->sender(); }273int bci = sd->bci();274if (bci != InvocationEntryBci) {275assert(map_length < pcds_in_method, "checking");276map[map_length].start_address = (const void*)pcd->real_pc(nm);277map[map_length].location = bci;278++map_length;279}280}281}282283*map_ptr = map;284*map_length_ptr = map_length;285}286287288