src/share/vm/prims/jvmtiCodeBlobEvents.cpp

Thu, 20 Aug 2009 12:42:57 -0700

author
cfang
date
Thu, 20 Aug 2009 12:42:57 -0700
changeset 1366
72088be4b386
parent 435
a61af66fc99e
child 1688
f70b0d9ab095
permissions
-rw-r--r--

6873116: Modify reexecute implementation to use pcDesc to record the reexecute bit
Summary: use PcDesc to keep record of the reexecute bit instead of using DebugInfoStreams
Reviewed-by: kvn, never, twisti

duke@435 1 /*
cfang@1366 2 * Copyright 2003-2009 Sun Microsystems, Inc. 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 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any 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()) {
duke@435 121 return;
duke@435 122 }
duke@435 123 }
duke@435 124
duke@435 125 // we must name the CodeBlob - some CodeBlobs already have names :-
duke@435 126 // - stubs used by compiled code to call a (static) C++ runtime routine
duke@435 127 // - non-relocatable machine code such as the interpreter, stubroutines, etc.
duke@435 128 // - various singleton blobs
duke@435 129 //
duke@435 130 // others are unnamed so we create a name :-
duke@435 131 // - OSR adapter (interpreter frame that has been on-stack replaced)
duke@435 132 // - I2C and C2I adapters
duke@435 133 const char* name = NULL;
duke@435 134 if (cb->is_runtime_stub()) {
duke@435 135 name = ((RuntimeStub*)cb)->name();
duke@435 136 }
duke@435 137 if (cb->is_buffer_blob()) {
duke@435 138 name = ((BufferBlob*)cb)->name();
duke@435 139 }
duke@435 140 if (cb->is_deoptimization_stub() || cb->is_safepoint_stub()) {
duke@435 141 name = ((SingletonBlob*)cb)->name();
duke@435 142 }
duke@435 143 if (cb->is_uncommon_trap_stub() || cb->is_exception_stub()) {
duke@435 144 name = ((SingletonBlob*)cb)->name();
duke@435 145 }
duke@435 146
duke@435 147 // record the CodeBlob details as a JvmtiCodeBlobDesc
duke@435 148 JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(name, cb->instructions_begin(),
duke@435 149 cb->instructions_end());
duke@435 150 _global_code_blobs->append(scb);
duke@435 151 }
duke@435 152
duke@435 153
duke@435 154 // collects a list of CodeBlobs in the CodeCache.
duke@435 155 //
duke@435 156 // The created list is growable array of JvmtiCodeBlobDesc - each one describes
duke@435 157 // a CodeBlob. Note that the list is static - this is because CodeBlob::blobs_do
duke@435 158 // requires a a C or static function so we can't use an instance function. This
duke@435 159 // isn't a problem as the iteration is serial anyway as we need the CodeCache_lock
duke@435 160 // to iterate over the code cache.
duke@435 161 //
duke@435 162 // Note that the CodeBlobs in the CodeCache will include BufferBlobs that may
duke@435 163 // contain multiple stubs. As a profiler is interested in the stubs rather than
duke@435 164 // the enclosing container we first iterate over the stub code descriptors so
duke@435 165 // that the stubs go into the list first. do_blob will then filter out the
duke@435 166 // enclosing blobs if the starting address of the enclosing blobs matches the
duke@435 167 // starting address of first stub generated in the enclosing blob.
duke@435 168
duke@435 169 void CodeBlobCollector::collect() {
duke@435 170 assert_locked_or_safepoint(CodeCache_lock);
duke@435 171 assert(_global_code_blobs == NULL, "checking");
duke@435 172
duke@435 173 // create the global list
duke@435 174 _global_code_blobs = new (ResourceObj::C_HEAP) GrowableArray<JvmtiCodeBlobDesc*>(50,true);
duke@435 175
duke@435 176 // iterate over the stub code descriptors and put them in the list first.
duke@435 177 int index = 0;
duke@435 178 StubCodeDesc* desc;
duke@435 179 while ((desc = StubCodeDesc::desc_for_index(++index)) != NULL) {
duke@435 180 _global_code_blobs->append(new JvmtiCodeBlobDesc(desc->name(), desc->begin(), desc->end()));
duke@435 181 }
duke@435 182
duke@435 183 // next iterate over all the non-nmethod code blobs and add them to
duke@435 184 // the list - as noted above this will filter out duplicates and
duke@435 185 // enclosing blobs.
duke@435 186 CodeCache::blobs_do(do_blob);
duke@435 187
duke@435 188 // make the global list the instance list so that it can be used
duke@435 189 // for other iterations.
duke@435 190 _code_blobs = _global_code_blobs;
duke@435 191 _global_code_blobs = NULL;
duke@435 192 }
duke@435 193
duke@435 194
duke@435 195 // Generate a DYNAMIC_CODE_GENERATED event for each non-nmethod code blob.
duke@435 196
duke@435 197 jvmtiError JvmtiCodeBlobEvents::generate_dynamic_code_events(JvmtiEnv* env) {
duke@435 198 CodeBlobCollector collector;
duke@435 199
duke@435 200 // first collect all the code blobs
duke@435 201 {
duke@435 202 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
duke@435 203 collector.collect();
duke@435 204 }
duke@435 205
duke@435 206 // iterate over the collected list and post an event for each blob
duke@435 207 JvmtiCodeBlobDesc* blob = collector.first();
duke@435 208 while (blob != NULL) {
duke@435 209 JvmtiExport::post_dynamic_code_generated(env, blob->name(), blob->code_begin(), blob->code_end());
duke@435 210 blob = collector.next();
duke@435 211 }
duke@435 212 return JVMTI_ERROR_NONE;
duke@435 213 }
duke@435 214
duke@435 215
duke@435 216 // Support class to describe a nmethod in the CodeCache
duke@435 217
duke@435 218 class nmethodDesc: public CHeapObj {
duke@435 219 private:
duke@435 220 methodHandle _method;
duke@435 221 address _code_begin;
duke@435 222 address _code_end;
duke@435 223 jvmtiAddrLocationMap* _map;
duke@435 224 jint _map_length;
duke@435 225 public:
duke@435 226 nmethodDesc(methodHandle method, address code_begin, address code_end,
duke@435 227 jvmtiAddrLocationMap* map, jint map_length) {
duke@435 228 _method = method;
duke@435 229 _code_begin = code_begin;
duke@435 230 _code_end = code_end;
duke@435 231 _map = map;
duke@435 232 _map_length = map_length;
duke@435 233 }
duke@435 234 methodHandle method() const { return _method; }
duke@435 235 address code_begin() const { return _code_begin; }
duke@435 236 address code_end() const { return _code_end; }
duke@435 237 jvmtiAddrLocationMap* map() const { return _map; }
duke@435 238 jint map_length() const { return _map_length; }
duke@435 239 };
duke@435 240
duke@435 241
duke@435 242 // Support class to collect a list of the nmethod CodeBlobs in
duke@435 243 // the CodeCache.
duke@435 244 //
duke@435 245 // Usage :-
duke@435 246 //
duke@435 247 // nmethodCollector collector;
duke@435 248 //
duke@435 249 // collector.collect();
duke@435 250 // JvmtiCodeBlobDesc* blob = collector.first();
duke@435 251 // while (blob != NULL) {
duke@435 252 // :
duke@435 253 // blob = collector.next();
duke@435 254 // }
duke@435 255 //
duke@435 256 class nmethodCollector : StackObj {
duke@435 257 private:
duke@435 258 GrowableArray<nmethodDesc*>* _nmethods; // collect nmethods
duke@435 259 int _pos; // iteration support
duke@435 260
duke@435 261 // used during a collection
duke@435 262 static GrowableArray<nmethodDesc*>* _global_nmethods;
duke@435 263 static void do_nmethod(nmethod* nm);
duke@435 264 public:
duke@435 265 nmethodCollector() {
duke@435 266 _nmethods = NULL;
duke@435 267 _pos = -1;
duke@435 268 }
duke@435 269 ~nmethodCollector() {
duke@435 270 if (_nmethods != NULL) {
duke@435 271 for (int i=0; i<_nmethods->length(); i++) {
duke@435 272 nmethodDesc* blob = _nmethods->at(i);
duke@435 273 if (blob->map()!= NULL) {
duke@435 274 FREE_C_HEAP_ARRAY(jvmtiAddrLocationMap, blob->map());
duke@435 275 }
duke@435 276 }
duke@435 277 delete _nmethods;
duke@435 278 }
duke@435 279 }
duke@435 280
duke@435 281 // collect list of nmethods in the cache
duke@435 282 void collect();
duke@435 283
duke@435 284 // iteration support - return first code blob
duke@435 285 nmethodDesc* first() {
duke@435 286 assert(_nmethods != NULL, "not collected");
duke@435 287 if (_nmethods->length() == 0) {
duke@435 288 return NULL;
duke@435 289 }
duke@435 290 _pos = 0;
duke@435 291 return _nmethods->at(0);
duke@435 292 }
duke@435 293
duke@435 294 // iteration support - return next code blob
duke@435 295 nmethodDesc* next() {
duke@435 296 assert(_pos >= 0, "iteration not started");
duke@435 297 if (_pos+1 >= _nmethods->length()) {
duke@435 298 return NULL;
duke@435 299 }
duke@435 300 return _nmethods->at(++_pos);
duke@435 301 }
duke@435 302 };
duke@435 303
duke@435 304 // used during collection
duke@435 305 GrowableArray<nmethodDesc*>* nmethodCollector::_global_nmethods;
duke@435 306
duke@435 307
duke@435 308 // called for each nmethod in the CodeCache
duke@435 309 //
duke@435 310 // This function simply adds a descriptor for each nmethod to the global list.
duke@435 311
duke@435 312 void nmethodCollector::do_nmethod(nmethod* nm) {
duke@435 313 // ignore zombies
duke@435 314 if (!nm->is_alive()) {
duke@435 315 return;
duke@435 316 }
duke@435 317
duke@435 318 assert(nm->method() != NULL, "checking");
duke@435 319
duke@435 320 // create the location map for the nmethod.
duke@435 321 jvmtiAddrLocationMap* map;
duke@435 322 jint map_length;
duke@435 323 JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nm, &map, &map_length);
duke@435 324
duke@435 325 // record the nmethod details
duke@435 326 methodHandle mh(nm->method());
duke@435 327 nmethodDesc* snm = new nmethodDesc(mh,
duke@435 328 nm->code_begin(),
duke@435 329 nm->code_end(),
duke@435 330 map,
duke@435 331 map_length);
duke@435 332 _global_nmethods->append(snm);
duke@435 333 }
duke@435 334
duke@435 335 // collects a list of nmethod in the CodeCache.
duke@435 336 //
duke@435 337 // The created list is growable array of nmethodDesc - each one describes
duke@435 338 // a nmethod and includs its JVMTI address location map.
duke@435 339
duke@435 340 void nmethodCollector::collect() {
duke@435 341 assert_locked_or_safepoint(CodeCache_lock);
duke@435 342 assert(_global_nmethods == NULL, "checking");
duke@435 343
duke@435 344 // create the list
duke@435 345 _global_nmethods = new (ResourceObj::C_HEAP) GrowableArray<nmethodDesc*>(100,true);
duke@435 346
duke@435 347 // any a descriptor for each nmethod to the list.
duke@435 348 CodeCache::nmethods_do(do_nmethod);
duke@435 349
duke@435 350 // make the list the instance list
duke@435 351 _nmethods = _global_nmethods;
duke@435 352 _global_nmethods = NULL;
duke@435 353 }
duke@435 354
duke@435 355 // Generate a COMPILED_METHOD_LOAD event for each nnmethod
duke@435 356
duke@435 357 jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) {
duke@435 358 HandleMark hm;
duke@435 359 nmethodCollector collector;
duke@435 360
duke@435 361 // first collect all nmethods
duke@435 362 {
duke@435 363 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
duke@435 364 collector.collect();
duke@435 365 }
duke@435 366
duke@435 367 // iterate over the list and post an event for each nmethod
duke@435 368 nmethodDesc* nm_desc = collector.first();
duke@435 369 while (nm_desc != NULL) {
duke@435 370 methodOop method = nm_desc->method()();
duke@435 371 jmethodID mid = method->jmethod_id();
duke@435 372 assert(mid != NULL, "checking");
duke@435 373 JvmtiExport::post_compiled_method_load(env, mid,
duke@435 374 (jint)(nm_desc->code_end() - nm_desc->code_begin()),
duke@435 375 nm_desc->code_begin(), nm_desc->map_length(),
duke@435 376 nm_desc->map());
duke@435 377 nm_desc = collector.next();
duke@435 378 }
duke@435 379 return JVMTI_ERROR_NONE;
duke@435 380 }
duke@435 381
duke@435 382
duke@435 383 // create a C-heap allocated address location map for an nmethod
duke@435 384 void JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nmethod *nm,
duke@435 385 jvmtiAddrLocationMap** map_ptr,
duke@435 386 jint *map_length_ptr)
duke@435 387 {
duke@435 388 ResourceMark rm;
duke@435 389 jvmtiAddrLocationMap* map = NULL;
duke@435 390 jint map_length = 0;
duke@435 391
duke@435 392
duke@435 393 // Generate line numbers using PcDesc and ScopeDesc info
duke@435 394 methodHandle mh(nm->method());
duke@435 395
duke@435 396 if (!mh->is_native()) {
duke@435 397 PcDesc *pcd;
duke@435 398 int pcds_in_method;
duke@435 399
duke@435 400 pcds_in_method = (nm->scopes_pcs_end() - nm->scopes_pcs_begin());
duke@435 401 map = NEW_C_HEAP_ARRAY(jvmtiAddrLocationMap, pcds_in_method);
duke@435 402
duke@435 403 address scopes_data = nm->scopes_data_begin();
duke@435 404 for( pcd = nm->scopes_pcs_begin(); pcd < nm->scopes_pcs_end(); ++pcd ) {
cfang@1366 405 ScopeDesc sc0(nm, pcd->scope_decode_offset(), pcd->should_reexecute());
duke@435 406 ScopeDesc *sd = &sc0;
duke@435 407 while( !sd->is_top() ) { sd = sd->sender(); }
duke@435 408 int bci = sd->bci();
duke@435 409 if (bci != InvocationEntryBci) {
duke@435 410 assert(map_length < pcds_in_method, "checking");
duke@435 411 map[map_length].start_address = (const void*)pcd->real_pc(nm);
duke@435 412 map[map_length].location = bci;
duke@435 413 ++map_length;
duke@435 414 }
duke@435 415 }
duke@435 416 }
duke@435 417
duke@435 418 *map_ptr = map;
duke@435 419 *map_length_ptr = map_length;
duke@435 420 }

mercurial