src/share/vm/prims/jvmtiCodeBlobEvents.cpp

Fri, 02 Jul 2010 15:01:47 -0700

author
never
date
Fri, 02 Jul 2010 15:01:47 -0700
changeset 1988
65b0c03b165d
parent 1971
38e8278318ca
child 2004
a528509c992b
permissions
-rw-r--r--

6965671: fatal error: acquiring lock JNIGlobalHandle_lock/16 out of order with lock CodeCache_lock/1
Reviewed-by: kvn, dcubed

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

mercurial