Path: blob/master/src/hotspot/share/prims/jvmtiCodeBlobEvents.cpp
41145 views
/*1* Copyright (c) 2003, 2020, 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/allocation.inline.hpp"30#include "memory/resourceArea.hpp"31#include "oops/oop.inline.hpp"32#include "prims/jvmtiCodeBlobEvents.hpp"33#include "prims/jvmtiExport.hpp"34#include "prims/jvmtiThreadState.inline.hpp"35#include "runtime/handles.inline.hpp"36#include "runtime/safepointVerifiers.hpp"37#include "runtime/stubCodeGenerator.hpp"38#include "runtime/vmThread.hpp"3940// Support class to collect a list of the non-nmethod CodeBlobs in41// the CodeCache.42//43// This class actually creates a list of JvmtiCodeBlobDesc - each JvmtiCodeBlobDesc44// describes a single CodeBlob in the CodeCache. Note that collection is45// done to a static list - this is because CodeCache::blobs_do is defined46// as void CodeCache::blobs_do(void f(CodeBlob* nm)) and hence requires47// a C or static method.48//49// Usage :-50//51// CodeBlobCollector collector;52//53// collector.collect();54// JvmtiCodeBlobDesc* blob = collector.first();55// while (blob != NULL) {56// :57// blob = collector.next();58// }59//6061class CodeBlobCollector : StackObj {62private:63GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs; // collected blobs64int _pos; // iterator position6566// used during a collection67static GrowableArray<JvmtiCodeBlobDesc*>* _global_code_blobs;68static void do_blob(CodeBlob* cb);69static void do_vtable_stub(VtableStub* vs);70public:71CodeBlobCollector() {72_code_blobs = NULL;73_pos = -1;74}75~CodeBlobCollector() {76if (_code_blobs != NULL) {77for (int i=0; i<_code_blobs->length(); i++) {78FreeHeap(_code_blobs->at(i));79}80delete _code_blobs;81}82}8384// collect list of code blobs in the cache85void collect();8687// iteration support - return first code blob88JvmtiCodeBlobDesc* first() {89assert(_code_blobs != NULL, "not collected");90if (_code_blobs->length() == 0) {91return NULL;92}93_pos = 0;94return _code_blobs->at(0);95}9697// iteration support - return next code blob98JvmtiCodeBlobDesc* next() {99assert(_pos >= 0, "iteration not started");100if (_pos+1 >= _code_blobs->length()) {101return NULL;102}103return _code_blobs->at(++_pos);104}105106};107108// used during collection109GrowableArray<JvmtiCodeBlobDesc*>* CodeBlobCollector::_global_code_blobs;110111112// called for each CodeBlob in the CodeCache113//114// This function filters out nmethods as it is only interested in115// other CodeBlobs. This function also filters out CodeBlobs that have116// a duplicate starting address as previous blobs. This is needed to117// handle the case where multiple stubs are generated into a single118// BufferBlob.119120void CodeBlobCollector::do_blob(CodeBlob* cb) {121122// ignore nmethods123if (cb->is_nmethod()) {124return;125}126// exclude VtableStubs, which are processed separately127if (cb->is_buffer_blob() && strcmp(cb->name(), "vtable chunks") == 0) {128return;129}130131// check if this starting address has been seen already - the132// assumption is that stubs are inserted into the list before the133// enclosing BufferBlobs.134address addr = cb->code_begin();135for (int i=0; i<_global_code_blobs->length(); i++) {136JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i);137if (addr == scb->code_begin()) {138return;139}140}141142// record the CodeBlob details as a JvmtiCodeBlobDesc143JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(cb->name(), cb->code_begin(), cb->code_end());144_global_code_blobs->append(scb);145}146147// called for each VtableStub in VtableStubs148149void CodeBlobCollector::do_vtable_stub(VtableStub* vs) {150JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(vs->is_vtable_stub() ? "vtable stub" : "itable stub",151vs->code_begin(), vs->code_end());152_global_code_blobs->append(scb);153}154155// collects a list of CodeBlobs in the CodeCache.156//157// The created list is growable array of JvmtiCodeBlobDesc - each one describes158// a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do159// requires a a C or static function so we can't use an instance function. This160// isn't a problem as the iteration is serial anyway as we need the CodeCache_lock161// to iterate over the code cache.162//163// Note that the CodeBlobs in the CodeCache will include BufferBlobs that may164// contain multiple stubs. As a profiler is interested in the stubs rather than165// the enclosing container we first iterate over the stub code descriptors so166// that the stubs go into the list first. do_blob will then filter out the167// enclosing blobs if the starting address of the enclosing blobs matches the168// starting address of first stub generated in the enclosing blob.169170void CodeBlobCollector::collect() {171assert_locked_or_safepoint(CodeCache_lock);172assert(_global_code_blobs == NULL, "checking");173174// create the global list175_global_code_blobs = new (ResourceObj::C_HEAP, mtServiceability) GrowableArray<JvmtiCodeBlobDesc*>(50, mtServiceability);176177// iterate over the stub code descriptors and put them in the list first.178for (StubCodeDesc* desc = StubCodeDesc::first(); desc != NULL; desc = StubCodeDesc::next(desc)) {179_global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end()));180}181182// Vtable stubs are not described with StubCodeDesc,183// process them separately184VtableStubs::vtable_stub_do(do_vtable_stub);185186// next iterate over all the non-nmethod code blobs and add them to187// the list - as noted above this will filter out duplicates and188// enclosing blobs.189CodeCache::blobs_do(do_blob);190191// make the global list the instance list so that it can be used192// for other iterations.193_code_blobs = _global_code_blobs;194_global_code_blobs = NULL;195}196197198// Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob.199200jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) {201CodeBlobCollector collector;202203// First collect all the code blobs. This has to be done in a204// single pass over the code cache with CodeCache_lock held because205// there isn't any safe way to iterate over regular CodeBlobs since206// they can be freed at any point.207{208MutexLocker mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);209collector.collect();210}211212// iterate over the collected list and post an event for each blob213JvmtiCodeBlobDesc* blob = collector.first();214while (blob != NULL) {215JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end());216blob = collector.next();217}218return JVMTI_ERROR_NONE;219}220221222// Generate a COMPILED_METHOD_LOAD event for each nnmethod223jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) {224JavaThread* java_thread = JavaThread::current();225JvmtiThreadState* state = JvmtiThreadState::state_for(java_thread);226{227NoSafepointVerifier nsv; // safepoints are not safe while collecting methods to post.228{229// Walk the CodeCache notifying for live nmethods, don't release the CodeCache_lock230// because the sweeper may be running concurrently.231// Save events to the queue for posting outside the CodeCache_lock.232MutexLocker mu(java_thread, CodeCache_lock, Mutex::_no_safepoint_check_flag);233// Iterate over non-profiled and profiled nmethods234NMethodIterator iter(NMethodIterator::only_alive_and_not_unloading);235while(iter.next()) {236nmethod* current = iter.method();237current->post_compiled_method_load_event(state);238}239}240241// Enter nmethod barrier code if present outside CodeCache_lock242state->run_nmethod_entry_barriers();243}244245// Now post all the events outside the CodeCache_lock.246// If there's a safepoint, the queued events will be kept alive.247// Adding these events to the service thread to post is something that248// should work, but the service thread doesn't keep up in stress scenarios and249// the os eventually kills the process with OOM.250// We want this thread to wait until the events are all posted.251state->post_events(env);252return JVMTI_ERROR_NONE;253}254255256// create a C-heap allocated address location map for an nmethod257void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm,258jvmtiAddrLocationMap** map_ptr,259jint *map_length_ptr)260{261ResourceMark rm;262jvmtiAddrLocationMap* map = NULL;263jint map_length = 0;264265266// Generate line numbers using PcDesc and ScopeDesc info267methodHandle mh(Thread::current(), nm->method());268269if (!mh->is_native()) {270PcDesc *pcd;271int pcds_in_method;272273pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin());274map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method, mtInternal);275276address scopes_data = nm->scopes_data_begin();277for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) {278ScopeDesc sc0(nm, pcd, true);279ScopeDesc *sd = &sc0;280while( !sd->is_top() ) { sd = sd->sender(); }281int bci = sd->bci();282if (bci >= 0) {283assert(map_length < pcds_in_method, "checking");284map[map_length].start_address = (const void*)pcd->real_pc(nm);285map[map_length].location = bci;286++map_length;287}288}289}290291*map_ptr = map;292*map_length_ptr = map_length;293}294295296