1 /* 2 * Copyright (c) 2001, 2010, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "ci/ciMethodData.hpp" 27 #include "ci/ciUtilities.hpp" 28 #include "memory/allocation.inline.hpp" 29 #include "memory/resourceArea.hpp" 30 #include "runtime/deoptimization.hpp" 31 #include "utilities/copy.hpp" 32 33 // ciMethodData 34 35 // ------------------------------------------------------------------ 36 // ciMethodData::ciMethodData 37 // 38 ciMethodData::ciMethodData(methodDataHandle h_md) : ciObject(h_md) { 39 assert(h_md() != NULL, "no null method data"); 40 Copy::zero_to_words((HeapWord*) &_orig, sizeof(_orig) / sizeof(HeapWord)); 41 _data = NULL; 42 _data_size = 0; 43 _extra_data_size = 0; 44 _current_mileage = 0; 45 _invocation_counter = 0; 46 _backedge_counter = 0; 47 _state = empty_state; 48 _saw_free_extra_data = false; 49 // Set an initial hint. Don't use set_hint_di() because 50 // first_di() may be out of bounds if data_size is 0. 51 _hint_di = first_di(); 52 // Initialize the escape information (to "don't know."); 53 _eflags = _arg_local = _arg_stack = _arg_returned = 0; 54 } 55 56 // ------------------------------------------------------------------ 57 // ciMethodData::ciMethodData 58 // 59 // No methodDataOop. 60 ciMethodData::ciMethodData() : ciObject() { 61 Copy::zero_to_words((HeapWord*) &_orig, sizeof(_orig) / sizeof(HeapWord)); 62 _data = NULL; 63 _data_size = 0; 64 _extra_data_size = 0; 65 _current_mileage = 0; 66 _invocation_counter = 0; 67 _backedge_counter = 0; 68 _state = empty_state; 69 _saw_free_extra_data = false; 70 // Set an initial hint. Don't use set_hint_di() because 71 // first_di() may be out of bounds if data_size is 0. 72 _hint_di = first_di(); 73 // Initialize the escape information (to "don't know."); 74 _eflags = _arg_local = _arg_stack = _arg_returned = 0; 75 } 76 77 void ciMethodData::load_data() { 78 methodDataOop mdo = get_methodDataOop(); 79 if (mdo == NULL) return; 80 81 // To do: don't copy the data if it is not "ripe" -- require a minimum # 82 // of invocations. 83 84 // Snapshot the data -- actually, take an approximate snapshot of 85 // the data. Any concurrently executing threads may be changing the 86 // data as we copy it. 87 int skip_header = oopDesc::header_size(); 88 Copy::disjoint_words((HeapWord*) mdo + skip_header, 89 (HeapWord*) &_orig + skip_header, 90 sizeof(_orig) / HeapWordSize - skip_header); 91 DEBUG_ONLY(*_orig.adr_method() = NULL); // no dangling oops, please 92 Arena* arena = CURRENT_ENV->arena(); 93 _data_size = mdo->data_size(); 94 _extra_data_size = mdo->extra_data_size(); 95 int total_size = _data_size + _extra_data_size; 96 _data = (intptr_t *) arena->Amalloc(total_size); 97 Copy::disjoint_words((HeapWord*) mdo->data_base(), (HeapWord*) _data, total_size / HeapWordSize); 98 99 // Traverse the profile data, translating any oops into their 100 // ci equivalents. 101 ResourceMark rm; 102 ciProfileData* ci_data = first_data(); 103 ProfileData* data = mdo->first_data(); 104 while (is_valid(ci_data)) { 105 ci_data->translate_from(data); 106 ci_data = next_data(ci_data); 107 data = mdo->next_data(data); 108 } 109 // Note: Extra data are all BitData, and do not need translation. 110 _current_mileage = methodDataOopDesc::mileage_of(mdo->method()); 111 _invocation_counter = mdo->invocation_count(); 112 _backedge_counter = mdo->backedge_count(); 113 _state = mdo->is_mature()? mature_state: immature_state; 114 115 _eflags = mdo->eflags(); 116 _arg_local = mdo->arg_local(); 117 _arg_stack = mdo->arg_stack(); 118 _arg_returned = mdo->arg_returned(); 119 } 120 121 void ciReceiverTypeData::translate_receiver_data_from(ProfileData* data) { 122 for (uint row = 0; row < row_limit(); row++) { 123 klassOop k = data->as_ReceiverTypeData()->receiver(row); 124 if (k != NULL) { 125 ciKlass* klass = CURRENT_ENV->get_object(k)->as_klass(); 126 set_receiver(row, klass); 127 } 128 } 129 } 130 131 132 // Get the data at an arbitrary (sort of) data index. 133 ciProfileData* ciMethodData::data_at(int data_index) { 134 if (out_of_bounds(data_index)) { 135 return NULL; 136 } 137 DataLayout* data_layout = data_layout_at(data_index); 138 139 switch (data_layout->tag()) { 140 case DataLayout::no_tag: 141 default: 142 ShouldNotReachHere(); 143 return NULL; 144 case DataLayout::bit_data_tag: 145 return new ciBitData(data_layout); 146 case DataLayout::counter_data_tag: 147 return new ciCounterData(data_layout); 148 case DataLayout::jump_data_tag: 149 return new ciJumpData(data_layout); 150 case DataLayout::receiver_type_data_tag: 151 return new ciReceiverTypeData(data_layout); 152 case DataLayout::virtual_call_data_tag: 153 return new ciVirtualCallData(data_layout); 154 case DataLayout::ret_data_tag: 155 return new ciRetData(data_layout); 156 case DataLayout::branch_data_tag: 157 return new ciBranchData(data_layout); 158 case DataLayout::multi_branch_data_tag: 159 return new ciMultiBranchData(data_layout); 160 case DataLayout::arg_info_data_tag: 161 return new ciArgInfoData(data_layout); 162 }; 163 } 164 165 // Iteration over data. 166 ciProfileData* ciMethodData::next_data(ciProfileData* current) { 167 int current_index = dp_to_di(current->dp()); 168 int next_index = current_index + current->size_in_bytes(); 169 ciProfileData* next = data_at(next_index); 170 return next; 171 } 172 173 // Translate a bci to its corresponding data, or NULL. 174 ciProfileData* ciMethodData::bci_to_data(int bci) { 175 ciProfileData* data = data_before(bci); 176 for ( ; is_valid(data); data = next_data(data)) { 177 if (data->bci() == bci) { 178 set_hint_di(dp_to_di(data->dp())); 179 return data; 180 } else if (data->bci() > bci) { 181 break; 182 } 183 } 184 // bci_to_extra_data(bci) ... 185 DataLayout* dp = data_layout_at(data_size()); 186 DataLayout* end = data_layout_at(data_size() + extra_data_size()); 187 for (; dp < end; dp = methodDataOopDesc::next_extra(dp)) { 188 if (dp->tag() == DataLayout::no_tag) { 189 _saw_free_extra_data = true; // observed an empty slot (common case) 190 return NULL; 191 } 192 if (dp->tag() == DataLayout::arg_info_data_tag) { 193 break; // ArgInfoData is at the end of extra data section. 194 } 195 if (dp->bci() == bci) { 196 assert(dp->tag() == DataLayout::bit_data_tag, "sane"); 197 return new ciBitData(dp); 198 } 199 } 200 return NULL; 201 } 202 203 // Conservatively decode the trap_state of a ciProfileData. 204 int ciMethodData::has_trap_at(ciProfileData* data, int reason) { 205 typedef Deoptimization::DeoptReason DR_t; 206 int per_bc_reason 207 = Deoptimization::reason_recorded_per_bytecode_if_any((DR_t) reason); 208 if (trap_count(reason) == 0) { 209 // Impossible for this trap to have occurred, regardless of trap_state. 210 // Note: This happens if the MDO is empty. 211 return 0; 212 } else if (per_bc_reason == Deoptimization::Reason_none) { 213 // We cannot conclude anything; a trap happened somewhere, maybe here. 214 return -1; 215 } else if (data == NULL) { 216 // No profile here, not even an extra_data record allocated on the fly. 217 // If there are empty extra_data records, and there had been a trap, 218 // there would have been a non-null data pointer. If there are no 219 // free extra_data records, we must return a conservative -1. 220 if (_saw_free_extra_data) 221 return 0; // Q.E.D. 222 else 223 return -1; // bail with a conservative answer 224 } else { 225 return Deoptimization::trap_state_has_reason(data->trap_state(), per_bc_reason); 226 } 227 } 228 229 int ciMethodData::trap_recompiled_at(ciProfileData* data) { 230 if (data == NULL) { 231 return (_saw_free_extra_data? 0: -1); // (see previous method) 232 } else { 233 return Deoptimization::trap_state_is_recompiled(data->trap_state())? 1: 0; 234 } 235 } 236 237 void ciMethodData::clear_escape_info() { 238 VM_ENTRY_MARK; 239 methodDataOop mdo = get_methodDataOop(); 240 if (mdo != NULL) { 241 mdo->clear_escape_info(); 242 ArgInfoData *aid = arg_info(); 243 int arg_count = (aid == NULL) ? 0 : aid->number_of_args(); 244 for (int i = 0; i < arg_count; i++) { 245 set_arg_modified(i, 0); 246 } 247 } 248 _eflags = _arg_local = _arg_stack = _arg_returned = 0; 249 } 250 251 // copy our escape info to the methodDataOop if it exists 252 void ciMethodData::update_escape_info() { 253 VM_ENTRY_MARK; 254 methodDataOop mdo = get_methodDataOop(); 255 if ( mdo != NULL) { 256 mdo->set_eflags(_eflags); 257 mdo->set_arg_local(_arg_local); 258 mdo->set_arg_stack(_arg_stack); 259 mdo->set_arg_returned(_arg_returned); 260 int arg_count = mdo->method()->size_of_parameters(); 261 for (int i = 0; i < arg_count; i++) { 262 mdo->set_arg_modified(i, arg_modified(i)); 263 } 264 } 265 } 266 267 void ciMethodData::set_compilation_stats(short loops, short blocks) { 268 VM_ENTRY_MARK; 269 methodDataOop mdo = get_methodDataOop(); 270 if (mdo != NULL) { 271 mdo->set_num_loops(loops); 272 mdo->set_num_blocks(blocks); 273 } 274 } 275 276 void ciMethodData::set_would_profile(bool p) { 277 VM_ENTRY_MARK; 278 methodDataOop mdo = get_methodDataOop(); 279 if (mdo != NULL) { 280 mdo->set_would_profile(p); 281 } 282 } 283 284 bool ciMethodData::has_escape_info() { 285 return eflag_set(methodDataOopDesc::estimated); 286 } 287 288 void ciMethodData::set_eflag(methodDataOopDesc::EscapeFlag f) { 289 set_bits(_eflags, f); 290 } 291 292 void ciMethodData::clear_eflag(methodDataOopDesc::EscapeFlag f) { 293 clear_bits(_eflags, f); 294 } 295 296 bool ciMethodData::eflag_set(methodDataOopDesc::EscapeFlag f) const { 297 return mask_bits(_eflags, f) != 0; 298 } 299 300 void ciMethodData::set_arg_local(int i) { 301 set_nth_bit(_arg_local, i); 302 } 303 304 void ciMethodData::set_arg_stack(int i) { 305 set_nth_bit(_arg_stack, i); 306 } 307 308 void ciMethodData::set_arg_returned(int i) { 309 set_nth_bit(_arg_returned, i); 310 } 311 312 void ciMethodData::set_arg_modified(int arg, uint val) { 313 ArgInfoData *aid = arg_info(); 314 if (aid == NULL) 315 return; 316 assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number"); 317 aid->set_arg_modified(arg, val); 318 } 319 320 bool ciMethodData::is_arg_local(int i) const { 321 return is_set_nth_bit(_arg_local, i); 322 } 323 324 bool ciMethodData::is_arg_stack(int i) const { 325 return is_set_nth_bit(_arg_stack, i); 326 } 327 328 bool ciMethodData::is_arg_returned(int i) const { 329 return is_set_nth_bit(_arg_returned, i); 330 } 331 332 uint ciMethodData::arg_modified(int arg) const { 333 ArgInfoData *aid = arg_info(); 334 if (aid == NULL) 335 return 0; 336 assert(arg >= 0 && arg < aid->number_of_args(), "valid argument number"); 337 return aid->arg_modified(arg); 338 } 339 340 ByteSize ciMethodData::offset_of_slot(ciProfileData* data, ByteSize slot_offset_in_data) { 341 // Get offset within methodDataOop of the data array 342 ByteSize data_offset = methodDataOopDesc::data_offset(); 343 344 // Get cell offset of the ProfileData within data array 345 int cell_offset = dp_to_di(data->dp()); 346 347 // Add in counter_offset, the # of bytes into the ProfileData of counter or flag 348 int offset = in_bytes(data_offset) + cell_offset + in_bytes(slot_offset_in_data); 349 350 return in_ByteSize(offset); 351 } 352 353 ciArgInfoData *ciMethodData::arg_info() const { 354 // Should be last, have to skip all traps. 355 DataLayout* dp = data_layout_at(data_size()); 356 DataLayout* end = data_layout_at(data_size() + extra_data_size()); 357 for (; dp < end; dp = methodDataOopDesc::next_extra(dp)) { 358 if (dp->tag() == DataLayout::arg_info_data_tag) 359 return new ciArgInfoData(dp); 360 } 361 return NULL; 362 } 363 364 365 // Implementation of the print method. 366 void ciMethodData::print_impl(outputStream* st) { 367 ciObject::print_impl(st); 368 } 369 370 #ifndef PRODUCT 371 void ciMethodData::print() { 372 print_data_on(tty); 373 } 374 375 void ciMethodData::print_data_on(outputStream* st) { 376 ResourceMark rm; 377 ciProfileData* data; 378 for (data = first_data(); is_valid(data); data = next_data(data)) { 379 st->print("%d", dp_to_di(data->dp())); 380 st->fill_to(6); 381 data->print_data_on(st); 382 } 383 st->print_cr("--- Extra data:"); 384 DataLayout* dp = data_layout_at(data_size()); 385 DataLayout* end = data_layout_at(data_size() + extra_data_size()); 386 for (; dp < end; dp = methodDataOopDesc::next_extra(dp)) { 387 if (dp->tag() == DataLayout::no_tag) continue; 388 if (dp->tag() == DataLayout::bit_data_tag) { 389 data = new BitData(dp); 390 } else { 391 assert(dp->tag() == DataLayout::arg_info_data_tag, "must be BitData or ArgInfo"); 392 data = new ciArgInfoData(dp); 393 dp = end; // ArgInfoData is at the end of extra data section. 394 } 395 st->print("%d", dp_to_di(data->dp())); 396 st->fill_to(6); 397 data->print_data_on(st); 398 } 399 } 400 401 void ciReceiverTypeData::print_receiver_data_on(outputStream* st) { 402 uint row; 403 int entries = 0; 404 for (row = 0; row < row_limit(); row++) { 405 if (receiver(row) != NULL) entries++; 406 } 407 st->print_cr("count(%u) entries(%u)", count(), entries); 408 for (row = 0; row < row_limit(); row++) { 409 if (receiver(row) != NULL) { 410 tab(st); 411 receiver(row)->print_name_on(st); 412 st->print_cr("(%u)", receiver_count(row)); 413 } 414 } 415 } 416 417 void ciReceiverTypeData::print_data_on(outputStream* st) { 418 print_shared(st, "ciReceiverTypeData"); 419 print_receiver_data_on(st); 420 } 421 422 void ciVirtualCallData::print_data_on(outputStream* st) { 423 print_shared(st, "ciVirtualCallData"); 424 rtd_super()->print_receiver_data_on(st); 425 } 426 #endif