1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/prims/jvmtiCodeBlobEvents.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,420 @@ 1.4 +/* 1.5 + * Copyright 2003-2007 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_jvmtiCodeBlobEvents.cpp.incl" 1.30 + 1.31 +// Support class to collect a list of the non-nmethod CodeBlobs in 1.32 +// the CodeCache. 1.33 +// 1.34 +// This class actually creates a list of JvmtiCodeBlobDesc - each JvmtiCodeBlobDesc 1.35 +// describes a single CodeBlob in the CodeCache. Note that collection is 1.36 +// done to a static list - this is because CodeCache::blobs_do is defined 1.37 +// as void CodeCache::blobs_do(void f(CodeBlob* nm)) and hence requires 1.38 +// a C or static method. 1.39 +// 1.40 +// Usage :- 1.41 +// 1.42 +// CodeBlobCollector collector; 1.43 +// 1.44 +// collector.collect(); 1.45 +// JvmtiCodeBlobDesc* blob = collector.first(); 1.46 +// while (blob != NULL) { 1.47 +// : 1.48 +// blob = collector.next(); 1.49 +// } 1.50 +// 1.51 + 1.52 +class CodeBlobCollector : StackObj { 1.53 + private: 1.54 + GrowableArray<JvmtiCodeBlobDesc*>* _code_blobs; // collected blobs 1.55 + int _pos; // iterator position 1.56 + 1.57 + // used during a collection 1.58 + static GrowableArray<JvmtiCodeBlobDesc*>* _global_code_blobs; 1.59 + static void do_blob(CodeBlob* cb); 1.60 + public: 1.61 + CodeBlobCollector() { 1.62 + _code_blobs = NULL; 1.63 + _pos = -1; 1.64 + } 1.65 + ~CodeBlobCollector() { 1.66 + if (_code_blobs != NULL) { 1.67 + for (int i=0; i<_code_blobs->length(); i++) { 1.68 + FreeHeap(_code_blobs->at(i)); 1.69 + } 1.70 + delete _code_blobs; 1.71 + } 1.72 + } 1.73 + 1.74 + // collect list of code blobs in the cache 1.75 + void collect(); 1.76 + 1.77 + // iteration support - return first code blob 1.78 + JvmtiCodeBlobDesc* first() { 1.79 + assert(_code_blobs != NULL, "not collected"); 1.80 + if (_code_blobs->length() == 0) { 1.81 + return NULL; 1.82 + } 1.83 + _pos = 0; 1.84 + return _code_blobs->at(0); 1.85 + } 1.86 + 1.87 + // iteration support - return next code blob 1.88 + JvmtiCodeBlobDesc* next() { 1.89 + assert(_pos >= 0, "iteration not started"); 1.90 + if (_pos+1 >= _code_blobs->length()) { 1.91 + return NULL; 1.92 + } 1.93 + return _code_blobs->at(++_pos); 1.94 + } 1.95 + 1.96 +}; 1.97 + 1.98 +// used during collection 1.99 +GrowableArray<JvmtiCodeBlobDesc*>* CodeBlobCollector::_global_code_blobs; 1.100 + 1.101 + 1.102 +// called for each CodeBlob in the CodeCache 1.103 +// 1.104 +// This function filters out nmethods as it is only interested in 1.105 +// other CodeBlobs. This function also filters out CodeBlobs that have 1.106 +// a duplicate starting address as previous blobs. This is needed to 1.107 +// handle the case where multiple stubs are generated into a single 1.108 +// BufferBlob. 1.109 + 1.110 +void CodeBlobCollector::do_blob(CodeBlob* cb) { 1.111 + 1.112 + // ignore nmethods 1.113 + if (cb->is_nmethod()) { 1.114 + return; 1.115 + } 1.116 + 1.117 + // check if this starting address has been seen already - the 1.118 + // assumption is that stubs are inserted into the list before the 1.119 + // enclosing BufferBlobs. 1.120 + address addr = cb->instructions_begin(); 1.121 + for (int i=0; i<_global_code_blobs->length(); i++) { 1.122 + JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i); 1.123 + if (addr == scb->code_begin()) { 1.124 + return; 1.125 + } 1.126 + } 1.127 + 1.128 + // we must name the CodeBlob - some CodeBlobs already have names :- 1.129 + // - stubs used by compiled code to call a (static) C++ runtime routine 1.130 + // - non-relocatable machine code such as the interpreter, stubroutines, etc. 1.131 + // - various singleton blobs 1.132 + // 1.133 + // others are unnamed so we create a name :- 1.134 + // - OSR adapter (interpreter frame that has been on-stack replaced) 1.135 + // - I2C and C2I adapters 1.136 + const char* name = NULL; 1.137 + if (cb->is_runtime_stub()) { 1.138 + name = ((RuntimeStub*)cb)->name(); 1.139 + } 1.140 + if (cb->is_buffer_blob()) { 1.141 + name = ((BufferBlob*)cb)->name(); 1.142 + } 1.143 + if (cb->is_deoptimization_stub() || cb->is_safepoint_stub()) { 1.144 + name = ((SingletonBlob*)cb)->name(); 1.145 + } 1.146 + if (cb->is_uncommon_trap_stub() || cb->is_exception_stub()) { 1.147 + name = ((SingletonBlob*)cb)->name(); 1.148 + } 1.149 + 1.150 + // record the CodeBlob details as a JvmtiCodeBlobDesc 1.151 + JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(name, cb->instructions_begin(), 1.152 + cb->instructions_end()); 1.153 + _global_code_blobs->append(scb); 1.154 +} 1.155 + 1.156 + 1.157 +// collects a list of CodeBlobs in the CodeCache. 1.158 +// 1.159 +// The created list is growable array of JvmtiCodeBlobDesc - each one describes 1.160 +// a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do 1.161 +// requires a a C or static function so we can't use an instance function. This 1.162 +// isn't a problem as the iteration is serial anyway as we need the CodeCache_lock 1.163 +// to iterate over the code cache. 1.164 +// 1.165 +// Note that the CodeBlobs in the CodeCache will include BufferBlobs that may 1.166 +// contain multiple stubs. As a profiler is interested in the stubs rather than 1.167 +// the enclosing container we first iterate over the stub code descriptors so 1.168 +// that the stubs go into the list first. do_blob will then filter out the 1.169 +// enclosing blobs if the starting address of the enclosing blobs matches the 1.170 +// starting address of first stub generated in the enclosing blob. 1.171 + 1.172 +void CodeBlobCollector::collect() { 1.173 + assert_locked_or_safepoint(CodeCache_lock); 1.174 + assert(_global_code_blobs == NULL, "checking"); 1.175 + 1.176 + // create the global list 1.177 + _global_code_blobs = new (ResourceObj::C_HEAP) GrowableArray<JvmtiCodeBlobDesc*>(50,true); 1.178 + 1.179 + // iterate over the stub code descriptors and put them in the list first. 1.180 + int index = 0; 1.181 + StubCodeDesc* desc; 1.182 + while ((desc = StubCodeDesc::desc_for_index(++index)) != NULL) { 1.183 + _global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end())); 1.184 + } 1.185 + 1.186 + // next iterate over all the non-nmethod code blobs and add them to 1.187 + // the list - as noted above this will filter out duplicates and 1.188 + // enclosing blobs. 1.189 + CodeCache::blobs_do(do_blob); 1.190 + 1.191 + // make the global list the instance list so that it can be used 1.192 + // for other iterations. 1.193 + _code_blobs = _global_code_blobs; 1.194 + _global_code_blobs = NULL; 1.195 +} 1.196 + 1.197 + 1.198 +// Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob. 1.199 + 1.200 +jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) { 1.201 + CodeBlobCollector collector; 1.202 + 1.203 + // first collect all the code blobs 1.204 + { 1.205 + MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 1.206 + collector.collect(); 1.207 + } 1.208 + 1.209 + // iterate over the collected list and post an event for each blob 1.210 + JvmtiCodeBlobDesc* blob = collector.first(); 1.211 + while (blob != NULL) { 1.212 + JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end()); 1.213 + blob = collector.next(); 1.214 + } 1.215 + return JVMTI_ERROR_NONE; 1.216 +} 1.217 + 1.218 + 1.219 +// Support class to describe a nmethod in the CodeCache 1.220 + 1.221 +class nmethodDesc: public CHeapObj { 1.222 + private: 1.223 + methodHandle _method; 1.224 + address _code_begin; 1.225 + address _code_end; 1.226 + jvmtiAddrLocationMap* _map; 1.227 + jint _map_length; 1.228 + public: 1.229 + nmethodDesc(methodHandle method, address code_begin, address code_end, 1.230 + jvmtiAddrLocationMap* map, jint map_length) { 1.231 + _method = method; 1.232 + _code_begin = code_begin; 1.233 + _code_end = code_end; 1.234 + _map = map; 1.235 + _map_length = map_length; 1.236 + } 1.237 + methodHandle method() const { return _method; } 1.238 + address code_begin() const { return _code_begin; } 1.239 + address code_end() const { return _code_end; } 1.240 + jvmtiAddrLocationMap* map() const { return _map; } 1.241 + jint map_length() const { return _map_length; } 1.242 +}; 1.243 + 1.244 + 1.245 +// Support class to collect a list of the nmethod CodeBlobs in 1.246 +// the CodeCache. 1.247 +// 1.248 +// Usage :- 1.249 +// 1.250 +// nmethodCollector collector; 1.251 +// 1.252 +// collector.collect(); 1.253 +// JvmtiCodeBlobDesc* blob = collector.first(); 1.254 +// while (blob != NULL) { 1.255 +// : 1.256 +// blob = collector.next(); 1.257 +// } 1.258 +// 1.259 +class nmethodCollector : StackObj { 1.260 + private: 1.261 + GrowableArray<nmethodDesc*>* _nmethods; // collect nmethods 1.262 + int _pos; // iteration support 1.263 + 1.264 + // used during a collection 1.265 + static GrowableArray<nmethodDesc*>* _global_nmethods; 1.266 + static void do_nmethod(nmethod* nm); 1.267 + public: 1.268 + nmethodCollector() { 1.269 + _nmethods = NULL; 1.270 + _pos = -1; 1.271 + } 1.272 + ~nmethodCollector() { 1.273 + if (_nmethods != NULL) { 1.274 + for (int i=0; i<_nmethods->length(); i++) { 1.275 + nmethodDesc* blob = _nmethods->at(i); 1.276 + if (blob->map()!= NULL) { 1.277 + FREE_C_HEAP_ARRAY(jvmtiAddrLocationMap, blob->map()); 1.278 + } 1.279 + } 1.280 + delete _nmethods; 1.281 + } 1.282 + } 1.283 + 1.284 + // collect list of nmethods in the cache 1.285 + void collect(); 1.286 + 1.287 + // iteration support - return first code blob 1.288 + nmethodDesc* first() { 1.289 + assert(_nmethods != NULL, "not collected"); 1.290 + if (_nmethods->length() == 0) { 1.291 + return NULL; 1.292 + } 1.293 + _pos = 0; 1.294 + return _nmethods->at(0); 1.295 + } 1.296 + 1.297 + // iteration support - return next code blob 1.298 + nmethodDesc* next() { 1.299 + assert(_pos >= 0, "iteration not started"); 1.300 + if (_pos+1 >= _nmethods->length()) { 1.301 + return NULL; 1.302 + } 1.303 + return _nmethods->at(++_pos); 1.304 + } 1.305 +}; 1.306 + 1.307 +// used during collection 1.308 +GrowableArray<nmethodDesc*>* nmethodCollector::_global_nmethods; 1.309 + 1.310 + 1.311 +// called for each nmethod in the CodeCache 1.312 +// 1.313 +// This function simply adds a descriptor for each nmethod to the global list. 1.314 + 1.315 +void nmethodCollector::do_nmethod(nmethod* nm) { 1.316 + // ignore zombies 1.317 + if (!nm->is_alive()) { 1.318 + return; 1.319 + } 1.320 + 1.321 + assert(nm->method() != NULL, "checking"); 1.322 + 1.323 + // create the location map for the nmethod. 1.324 + jvmtiAddrLocationMap* map; 1.325 + jint map_length; 1.326 + JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nm, &map, &map_length); 1.327 + 1.328 + // record the nmethod details 1.329 + methodHandle mh(nm->method()); 1.330 + nmethodDesc* snm = new nmethodDesc(mh, 1.331 + nm->code_begin(), 1.332 + nm->code_end(), 1.333 + map, 1.334 + map_length); 1.335 + _global_nmethods->append(snm); 1.336 +} 1.337 + 1.338 +// collects a list of nmethod in the CodeCache. 1.339 +// 1.340 +// The created list is growable array of nmethodDesc - each one describes 1.341 +// a nmethod and includs its JVMTI address location map. 1.342 + 1.343 +void nmethodCollector::collect() { 1.344 + assert_locked_or_safepoint(CodeCache_lock); 1.345 + assert(_global_nmethods == NULL, "checking"); 1.346 + 1.347 + // create the list 1.348 + _global_nmethods = new (ResourceObj::C_HEAP) GrowableArray<nmethodDesc*>(100,true); 1.349 + 1.350 + // any a descriptor for each nmethod to the list. 1.351 + CodeCache::nmethods_do(do_nmethod); 1.352 + 1.353 + // make the list the instance list 1.354 + _nmethods = _global_nmethods; 1.355 + _global_nmethods = NULL; 1.356 +} 1.357 + 1.358 +// Generate a COMPILED_METHOD_LOAD event for each nnmethod 1.359 + 1.360 +jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) { 1.361 + HandleMark hm; 1.362 + nmethodCollector collector; 1.363 + 1.364 + // first collect all nmethods 1.365 + { 1.366 + MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); 1.367 + collector.collect(); 1.368 + } 1.369 + 1.370 + // iterate over the list and post an event for each nmethod 1.371 + nmethodDesc* nm_desc = collector.first(); 1.372 + while (nm_desc != NULL) { 1.373 + methodOop method = nm_desc->method()(); 1.374 + jmethodID mid = method->jmethod_id(); 1.375 + assert(mid != NULL, "checking"); 1.376 + JvmtiExport::post_compiled_method_load(env, mid, 1.377 + (jint)(nm_desc->code_end() - nm_desc->code_begin()), 1.378 + nm_desc->code_begin(), nm_desc->map_length(), 1.379 + nm_desc->map()); 1.380 + nm_desc = collector.next(); 1.381 + } 1.382 + return JVMTI_ERROR_NONE; 1.383 +} 1.384 + 1.385 + 1.386 +// create a C-heap allocated address location map for an nmethod 1.387 +void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm, 1.388 + jvmtiAddrLocationMap** map_ptr, 1.389 + jint *map_length_ptr) 1.390 +{ 1.391 + ResourceMark rm; 1.392 + jvmtiAddrLocationMap* map = NULL; 1.393 + jint map_length = 0; 1.394 + 1.395 + 1.396 + // Generate line numbers using PcDesc and ScopeDesc info 1.397 + methodHandle mh(nm->method()); 1.398 + 1.399 + if (!mh->is_native()) { 1.400 + PcDesc *pcd; 1.401 + int pcds_in_method; 1.402 + 1.403 + pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin()); 1.404 + map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method); 1.405 + 1.406 + address scopes_data = nm->scopes_data_begin(); 1.407 + for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) { 1.408 + ScopeDesc sc0(nm, pcd->scope_decode_offset()); 1.409 + ScopeDesc *sd = &sc0; 1.410 + while( !sd->is_top() ) { sd = sd->sender(); } 1.411 + int bci = sd->bci(); 1.412 + if (bci != InvocationEntryBci) { 1.413 + assert(map_length < pcds_in_method, "checking"); 1.414 + map[map_length].start_address = (const void*)pcd->real_pc(nm); 1.415 + map[map_length].location = bci; 1.416 + ++map_length; 1.417 + } 1.418 + } 1.419 + } 1.420 + 1.421 + *map_ptr = map; 1.422 + *map_length_ptr = map_length; 1.423 +}