src/share/vm/prims/jvmtiCodeBlobEvents.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6310
22b3b2f888bc
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "code/codeBlob.hpp"
aoqi@0 27 #include "code/codeCache.hpp"
aoqi@0 28 #include "code/scopeDesc.hpp"
aoqi@0 29 #include "code/vtableStubs.hpp"
aoqi@0 30 #include "memory/resourceArea.hpp"
aoqi@0 31 #include "oops/oop.inline.hpp"
aoqi@0 32 #include "prims/jvmtiCodeBlobEvents.hpp"
aoqi@0 33 #include "prims/jvmtiExport.hpp"
aoqi@0 34 #include "runtime/handles.hpp"
aoqi@0 35 #include "runtime/handles.inline.hpp"
aoqi@0 36 #include "runtime/vmThread.hpp"
aoqi@0 37
aoqi@0 38 // Support class to collect a list of the non-nmethod CodeBlobs in
aoqi@0 39 // the CodeCache.
aoqi@0 40 //
aoqi@0 41 // This class actually creates a list of JvmtiCodeBlobDesc - each JvmtiCodeBlobDesc
aoqi@0 42 // describes a single CodeBlob in the CodeCache. Note that collection is
aoqi@0 43 // done to a static list - this is because CodeCache::blobs_do is defined
aoqi@0 44 // as void CodeCache::blobs_do(void f(CodeBlob* nm)) and hence requires
aoqi@0 45 // a C or static method.
aoqi@0 46 //
aoqi@0 47 // Usage :-
aoqi@0 48 //
aoqi@0 49 // CodeBlobCollector collector;
aoqi@0 50 //
aoqi@0 51 // collector.collect();
aoqi@0 52 // JvmtiCodeBlobDesc* blob = collector.first();
aoqi@0 53 // while (blob != NULL) {
aoqi@0 54 // :
aoqi@0 55 // blob = collector.next();
aoqi@0 56 // }
aoqi@0 57 //
aoqi@0 58
aoqi@0 59 class CodeBlobCollector : StackObj {
aoqi@0 60 private:
aoqi@0 61 GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs; // collected blobs
aoqi@0 62 int _pos; // iterator position
aoqi@0 63
aoqi@0 64 // used during a collection
aoqi@0 65 static GrowableArray<JvmtiCodeBlobDesc*>* _global_code_blobs;
aoqi@0 66 static void do_blob(CodeBlob* cb);
aoqi@0 67 static void do_vtable_stub(VtableStub* vs);
aoqi@0 68 public:
aoqi@0 69 CodeBlobCollector() {
aoqi@0 70 _code_blobs = NULL;
aoqi@0 71 _pos = -1;
aoqi@0 72 }
aoqi@0 73 ~CodeBlobCollector() {
aoqi@0 74 if (_code_blobs != NULL) {
aoqi@0 75 for (int i=0; i<_code_blobs->length(); i++) {
aoqi@0 76 FreeHeap(_code_blobs->at(i));
aoqi@0 77 }
aoqi@0 78 delete _code_blobs;
aoqi@0 79 }
aoqi@0 80 }
aoqi@0 81
aoqi@0 82 // collect list of code blobs in the cache
aoqi@0 83 void collect();
aoqi@0 84
aoqi@0 85 // iteration support - return first code blob
aoqi@0 86 JvmtiCodeBlobDesc* first() {
aoqi@0 87 assert(_code_blobs != NULL, "not collected");
aoqi@0 88 if (_code_blobs->length() == 0) {
aoqi@0 89 return NULL;
aoqi@0 90 }
aoqi@0 91 _pos = 0;
aoqi@0 92 return _code_blobs->at(0);
aoqi@0 93 }
aoqi@0 94
aoqi@0 95 // iteration support - return next code blob
aoqi@0 96 JvmtiCodeBlobDesc* next() {
aoqi@0 97 assert(_pos >= 0, "iteration not started");
aoqi@0 98 if (_pos+1 >= _code_blobs->length()) {
aoqi@0 99 return NULL;
aoqi@0 100 }
aoqi@0 101 return _code_blobs->at(++_pos);
aoqi@0 102 }
aoqi@0 103
aoqi@0 104 };
aoqi@0 105
aoqi@0 106 // used during collection
aoqi@0 107 GrowableArray<JvmtiCodeBlobDesc*>* CodeBlobCollector::_global_code_blobs;
aoqi@0 108
aoqi@0 109
aoqi@0 110 // called for each CodeBlob in the CodeCache
aoqi@0 111 //
aoqi@0 112 // This function filters out nmethods as it is only interested in
aoqi@0 113 // other CodeBlobs. This function also filters out CodeBlobs that have
aoqi@0 114 // a duplicate starting address as previous blobs. This is needed to
aoqi@0 115 // handle the case where multiple stubs are generated into a single
aoqi@0 116 // BufferBlob.
aoqi@0 117
aoqi@0 118 void CodeBlobCollector::do_blob(CodeBlob* cb) {
aoqi@0 119
aoqi@0 120 // ignore nmethods
aoqi@0 121 if (cb->is_nmethod()) {
aoqi@0 122 return;
aoqi@0 123 }
aoqi@0 124 // exclude VtableStubs, which are processed separately
aoqi@0 125 if (cb->is_buffer_blob() && strcmp(cb->name(), "vtable chunks") == 0) {
aoqi@0 126 return;
aoqi@0 127 }
aoqi@0 128
aoqi@0 129 // check if this starting address has been seen already - the
aoqi@0 130 // assumption is that stubs are inserted into the list before the
aoqi@0 131 // enclosing BufferBlobs.
aoqi@0 132 address addr = cb->code_begin();
aoqi@0 133 for (int i=0; i<_global_code_blobs->length(); i++) {
aoqi@0 134 JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i);
aoqi@0 135 if (addr == scb->code_begin()) {
aoqi@0 136 return;
aoqi@0 137 }
aoqi@0 138 }
aoqi@0 139
aoqi@0 140 // record the CodeBlob details as a JvmtiCodeBlobDesc
aoqi@0 141 JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(cb->name(), cb->code_begin(), cb->code_end());
aoqi@0 142 _global_code_blobs->append(scb);
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 // called for each VtableStub in VtableStubs
aoqi@0 146
aoqi@0 147 void CodeBlobCollector::do_vtable_stub(VtableStub* vs) {
aoqi@0 148 JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(vs->is_vtable_stub() ? "vtable stub" : "itable stub",
aoqi@0 149 vs->code_begin(), vs->code_end());
aoqi@0 150 _global_code_blobs->append(scb);
aoqi@0 151 }
aoqi@0 152
aoqi@0 153 // collects a list of CodeBlobs in the CodeCache.
aoqi@0 154 //
aoqi@0 155 // The created list is growable array of JvmtiCodeBlobDesc - each one describes
aoqi@0 156 // a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do
aoqi@0 157 // requires a a C or static function so we can't use an instance function. This
aoqi@0 158 // isn't a problem as the iteration is serial anyway as we need the CodeCache_lock
aoqi@0 159 // to iterate over the code cache.
aoqi@0 160 //
aoqi@0 161 // Note that the CodeBlobs in the CodeCache will include BufferBlobs that may
aoqi@0 162 // contain multiple stubs. As a profiler is interested in the stubs rather than
aoqi@0 163 // the enclosing container we first iterate over the stub code descriptors so
aoqi@0 164 // that the stubs go into the list first. do_blob will then filter out the
aoqi@0 165 // enclosing blobs if the starting address of the enclosing blobs matches the
aoqi@0 166 // starting address of first stub generated in the enclosing blob.
aoqi@0 167
aoqi@0 168 void CodeBlobCollector::collect() {
aoqi@0 169 assert_locked_or_safepoint(CodeCache_lock);
aoqi@0 170 assert(_global_code_blobs == NULL, "checking");
aoqi@0 171
aoqi@0 172 // create the global list
aoqi@0 173 _global_code_blobs = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<JvmtiCodeBlobDesc*>(50,true);
aoqi@0 174
aoqi@0 175 // iterate over the stub code descriptors and put them in the list first.
aoqi@0 176 int index = 0;
aoqi@0 177 StubCodeDesc* desc;
aoqi@0 178 while ((desc = StubCodeDesc::desc_for_index(++index)) != NULL) {
aoqi@0 179 _global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end()));
aoqi@0 180 }
aoqi@0 181
aoqi@0 182 // Vtable stubs are not described with StubCodeDesc,
aoqi@0 183 // process them separately
aoqi@0 184 VtableStubs::vtable_stub_do(do_vtable_stub);
aoqi@0 185
aoqi@0 186 // next iterate over all the non-nmethod code blobs and add them to
aoqi@0 187 // the list - as noted above this will filter out duplicates and
aoqi@0 188 // enclosing blobs.
aoqi@0 189 CodeCache::blobs_do(do_blob);
aoqi@0 190
aoqi@0 191 // make the global list the instance list so that it can be used
aoqi@0 192 // for other iterations.
aoqi@0 193 _code_blobs = _global_code_blobs;
aoqi@0 194 _global_code_blobs = NULL;
aoqi@0 195 }
aoqi@0 196
aoqi@0 197
aoqi@0 198 // Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob.
aoqi@0 199
aoqi@0 200 jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) {
aoqi@0 201 CodeBlobCollector collector;
aoqi@0 202
aoqi@0 203 // First collect all the code blobs. This has to be done in a
aoqi@0 204 // single pass over the code cache with CodeCache_lock held because
aoqi@0 205 // there isn't any safe way to iterate over regular CodeBlobs since
aoqi@0 206 // they can be freed at any point.
aoqi@0 207 {
aoqi@0 208 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 209 collector.collect();
aoqi@0 210 }
aoqi@0 211
aoqi@0 212 // iterate over the collected list and post an event for each blob
aoqi@0 213 JvmtiCodeBlobDesc* blob = collector.first();
aoqi@0 214 while (blob != NULL) {
aoqi@0 215 JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end());
aoqi@0 216 blob = collector.next();
aoqi@0 217 }
aoqi@0 218 return JVMTI_ERROR_NONE;
aoqi@0 219 }
aoqi@0 220
aoqi@0 221
aoqi@0 222 // Generate a COMPILED_METHOD_LOAD event for each nnmethod
aoqi@0 223 jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) {
aoqi@0 224 HandleMark hm;
aoqi@0 225
aoqi@0 226 // Walk the CodeCache notifying for live nmethods. The code cache
aoqi@0 227 // may be changing while this is happening which is ok since newly
aoqi@0 228 // created nmethod will notify normally and nmethods which are freed
aoqi@0 229 // can be safely skipped.
aoqi@0 230 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 231 nmethod* current = CodeCache::first_nmethod();
aoqi@0 232 while (current != NULL) {
aoqi@0 233 // Only notify for live nmethods
aoqi@0 234 if (current->is_alive()) {
aoqi@0 235 // Lock the nmethod so it can't be freed
aoqi@0 236 nmethodLocker nml(current);
aoqi@0 237
aoqi@0 238 // Don't hold the lock over the notify or jmethodID creation
aoqi@0 239 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 240 current->get_and_cache_jmethod_id();
aoqi@0 241 JvmtiExport::post_compiled_method_load(current);
aoqi@0 242 }
aoqi@0 243 current = CodeCache::next_nmethod(current);
aoqi@0 244 }
aoqi@0 245 return JVMTI_ERROR_NONE;
aoqi@0 246 }
aoqi@0 247
aoqi@0 248
aoqi@0 249 // create a C-heap allocated address location map for an nmethod
aoqi@0 250 void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm,
aoqi@0 251 jvmtiAddrLocationMap** map_ptr,
aoqi@0 252 jint *map_length_ptr)
aoqi@0 253 {
aoqi@0 254 ResourceMark rm;
aoqi@0 255 jvmtiAddrLocationMap* map = NULL;
aoqi@0 256 jint map_length = 0;
aoqi@0 257
aoqi@0 258
aoqi@0 259 // Generate line numbers using PcDesc and ScopeDesc info
aoqi@0 260 methodHandle mh(nm->method());
aoqi@0 261
aoqi@0 262 if (!mh->is_native()) {
aoqi@0 263 PcDesc *pcd;
aoqi@0 264 int pcds_in_method;
aoqi@0 265
aoqi@0 266 pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin());
aoqi@0 267 map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method, mtInternal);
aoqi@0 268
aoqi@0 269 address scopes_data = nm->scopes_data_begin();
aoqi@0 270 for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) {
aoqi@0 271 ScopeDesc sc0(nm, pcd->scope_decode_offset(), pcd->should_reexecute(), pcd->return_oop());
aoqi@0 272 ScopeDesc *sd = &sc0;
aoqi@0 273 while( !sd->is_top() ) { sd = sd->sender(); }
aoqi@0 274 int bci = sd->bci();
aoqi@0 275 if (bci != InvocationEntryBci) {
aoqi@0 276 assert(map_length < pcds_in_method, "checking");
aoqi@0 277 map[map_length].start_address = (const void*)pcd->real_pc(nm);
aoqi@0 278 map[map_length].location = bci;
aoqi@0 279 ++map_length;
aoqi@0 280 }
aoqi@0 281 }
aoqi@0 282 }
aoqi@0 283
aoqi@0 284 *map_ptr = map;
aoqi@0 285 *map_length_ptr = map_length;
aoqi@0 286 }

mercurial