src/share/vm/prims/jvmtiCodeBlobEvents.cpp

Wed, 25 Aug 2010 05:27:54 -0700

author
twisti
date
Wed, 25 Aug 2010 05:27:54 -0700
changeset 2103
3e8fbc61cee8
parent 2004
a528509c992b
child 2314
f95d63e2154a
permissions
-rw-r--r--

6978355: renaming for 6961697
Summary: This is the renaming part of 6961697 to keep the actual changes small for review.
Reviewed-by: kvn, never

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.
twisti@2103 117 address addr = cb->code_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()) {
duke@435 121 return;
duke@435 122 }
duke@435 123 }
duke@435 124
duke@435 125 // record the CodeBlob details as a JvmtiCodeBlobDesc
twisti@2103 126 JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(cb->name(), cb->code_begin(), cb->code_end());
duke@435 127 _global_code_blobs->append(scb);
duke@435 128 }
duke@435 129
duke@435 130
duke@435 131 // collects a list of CodeBlobs in the CodeCache.
duke@435 132 //
duke@435 133 // The created list is growable array of JvmtiCodeBlobDesc - each one describes
duke@435 134 // a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do
duke@435 135 // requires a a C or static function so we can't use an instance function. This
duke@435 136 // isn't a problem as the iteration is serial anyway as we need the CodeCache_lock
duke@435 137 // to iterate over the code cache.
duke@435 138 //
duke@435 139 // Note that the CodeBlobs in the CodeCache will include BufferBlobs that may
duke@435 140 // contain multiple stubs. As a profiler is interested in the stubs rather than
duke@435 141 // the enclosing container we first iterate over the stub code descriptors so
duke@435 142 // that the stubs go into the list first. do_blob will then filter out the
duke@435 143 // enclosing blobs if the starting address of the enclosing blobs matches the
duke@435 144 // starting address of first stub generated in the enclosing blob.
duke@435 145
duke@435 146 void CodeBlobCollector::collect() {
duke@435 147 assert_locked_or_safepoint(CodeCache_lock);
duke@435 148 assert(_global_code_blobs == NULL, "checking");
duke@435 149
duke@435 150 // create the global list
duke@435 151 _global_code_blobs = new (ResourceObj::C_HEAP) GrowableArray<JvmtiCodeBlobDesc*>(50,true);
duke@435 152
duke@435 153 // iterate over the stub code descriptors and put them in the list first.
duke@435 154 int index = 0;
duke@435 155 StubCodeDesc* desc;
duke@435 156 while ((desc = StubCodeDesc::desc_for_index(++index)) != NULL) {
duke@435 157 _global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end()));
duke@435 158 }
duke@435 159
duke@435 160 // next iterate over all the non-nmethod code blobs and add them to
duke@435 161 // the list - as noted above this will filter out duplicates and
duke@435 162 // enclosing blobs.
duke@435 163 CodeCache::blobs_do(do_blob);
duke@435 164
duke@435 165 // make the global list the instance list so that it can be used
duke@435 166 // for other iterations.
duke@435 167 _code_blobs = _global_code_blobs;
duke@435 168 _global_code_blobs = NULL;
duke@435 169 }
duke@435 170
duke@435 171
duke@435 172 // Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob.
duke@435 173
duke@435 174 jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) {
duke@435 175 CodeBlobCollector collector;
duke@435 176
never@1988 177 // First collect all the code blobs. This has to be done in a
never@1988 178 // single pass over the code cache with CodeCache_lock held because
never@1988 179 // there isn't any safe way to iterate over regular CodeBlobs since
never@1988 180 // they can be freed at any point.
duke@435 181 {
duke@435 182 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
duke@435 183 collector.collect();
duke@435 184 }
duke@435 185
duke@435 186 // iterate over the collected list and post an event for each blob
duke@435 187 JvmtiCodeBlobDesc* blob = collector.first();
duke@435 188 while (blob != NULL) {
duke@435 189 JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end());
duke@435 190 blob = collector.next();
duke@435 191 }
duke@435 192 return JVMTI_ERROR_NONE;
duke@435 193 }
duke@435 194
duke@435 195
duke@435 196 // Generate a COMPILED_METHOD_LOAD event for each nnmethod
duke@435 197 jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) {
duke@435 198 HandleMark hm;
duke@435 199
never@1988 200 // Walk the CodeCache notifying for live nmethods. The code cache
never@1988 201 // may be changing while this is happening which is ok since newly
never@1988 202 // created nmethod will notify normally and nmethods which are freed
never@1988 203 // can be safely skipped.
never@1988 204 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
never@1988 205 nmethod* current = CodeCache::first_nmethod();
never@1988 206 while (current != NULL) {
never@1988 207 // Only notify for live nmethods
never@1988 208 if (current->is_alive()) {
never@2004 209 // Lock the nmethod so it can't be freed
never@2004 210 nmethodLocker nml(current);
never@2004 211
never@1988 212 // Don't hold the lock over the notify or jmethodID creation
never@1988 213 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
never@1988 214 current->get_and_cache_jmethod_id();
never@1988 215 JvmtiExport::post_compiled_method_load(current);
never@1988 216 }
never@1988 217 current = CodeCache::next_nmethod(current);
duke@435 218 }
duke@435 219 return JVMTI_ERROR_NONE;
duke@435 220 }
duke@435 221
duke@435 222
duke@435 223 // create a C-heap allocated address location map for an nmethod
duke@435 224 void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm,
duke@435 225 jvmtiAddrLocationMap** map_ptr,
duke@435 226 jint *map_length_ptr)
duke@435 227 {
duke@435 228 ResourceMark rm;
duke@435 229 jvmtiAddrLocationMap* map = NULL;
duke@435 230 jint map_length = 0;
duke@435 231
duke@435 232
duke@435 233 // Generate line numbers using PcDesc and ScopeDesc info
duke@435 234 methodHandle mh(nm->method());
duke@435 235
duke@435 236 if (!mh->is_native()) {
duke@435 237 PcDesc *pcd;
duke@435 238 int pcds_in_method;
duke@435 239
duke@435 240 pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin());
duke@435 241 map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method);
duke@435 242
duke@435 243 address scopes_data = nm->scopes_data_begin();
duke@435 244 for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) {
kvn@1688 245 ScopeDesc sc0(nm, pcd->scope_decode_offset(), pcd->should_reexecute(), pcd->return_oop());
duke@435 246 ScopeDesc *sd = &sc0;
duke@435 247 while( !sd->is_top() ) { sd = sd->sender(); }
duke@435 248 int bci = sd->bci();
duke@435 249 if (bci != InvocationEntryBci) {
duke@435 250 assert(map_length < pcds_in_method, "checking");
duke@435 251 map[map_length].start_address = (const void*)pcd->real_pc(nm);
duke@435 252 map[map_length].location = bci;
duke@435 253 ++map_length;
duke@435 254 }
duke@435 255 }
duke@435 256 }
duke@435 257
duke@435 258 *map_ptr = map;
duke@435 259 *map_length_ptr = map_length;
duke@435 260 }

mercurial