Wed, 03 Sep 2014 09:25:44 +0200
Merge
1 /*
2 * Copyright (c) 2000, 2014, 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 */
25 #include "precompiled.hpp"
26 #include "classfile/systemDictionary.hpp"
27 #include "compiler/compilerOracle.hpp"
28 #include "interpreter/bytecode.hpp"
29 #include "interpreter/bytecodeStream.hpp"
30 #include "interpreter/linkResolver.hpp"
31 #include "memory/heapInspection.hpp"
32 #include "oops/methodData.hpp"
33 #include "prims/jvmtiRedefineClasses.hpp"
34 #include "runtime/compilationPolicy.hpp"
35 #include "runtime/deoptimization.hpp"
36 #include "runtime/handles.inline.hpp"
37 #include "runtime/orderAccess.inline.hpp"
39 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
41 // ==================================================================
42 // DataLayout
43 //
44 // Overlay for generic profiling data.
46 // Some types of data layouts need a length field.
47 bool DataLayout::needs_array_len(u1 tag) {
48 return (tag == multi_branch_data_tag) || (tag == arg_info_data_tag) || (tag == parameters_type_data_tag);
49 }
51 // Perform generic initialization of the data. More specific
52 // initialization occurs in overrides of ProfileData::post_initialize.
53 void DataLayout::initialize(u1 tag, u2 bci, int cell_count) {
54 _header._bits = (intptr_t)0;
55 _header._struct._tag = tag;
56 _header._struct._bci = bci;
57 for (int i = 0; i < cell_count; i++) {
58 set_cell_at(i, (intptr_t)0);
59 }
60 if (needs_array_len(tag)) {
61 set_cell_at(ArrayData::array_len_off_set, cell_count - 1); // -1 for header.
62 }
63 if (tag == call_type_data_tag) {
64 CallTypeData::initialize(this, cell_count);
65 } else if (tag == virtual_call_type_data_tag) {
66 VirtualCallTypeData::initialize(this, cell_count);
67 }
68 }
70 void DataLayout::clean_weak_klass_links(BoolObjectClosure* cl) {
71 ResourceMark m;
72 data_in()->clean_weak_klass_links(cl);
73 }
76 // ==================================================================
77 // ProfileData
78 //
79 // A ProfileData object is created to refer to a section of profiling
80 // data in a structured way.
82 // Constructor for invalid ProfileData.
83 ProfileData::ProfileData() {
84 _data = NULL;
85 }
87 char* ProfileData::print_data_on_helper(const MethodData* md) const {
88 DataLayout* dp = md->extra_data_base();
89 DataLayout* end = md->extra_data_limit();
90 stringStream ss;
91 for (;; dp = MethodData::next_extra(dp)) {
92 assert(dp < end, "moved past end of extra data");
93 switch(dp->tag()) {
94 case DataLayout::speculative_trap_data_tag:
95 if (dp->bci() == bci()) {
96 SpeculativeTrapData* data = new SpeculativeTrapData(dp);
97 int trap = data->trap_state();
98 char buf[100];
99 ss.print("trap/");
100 data->method()->print_short_name(&ss);
101 ss.print("(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
102 }
103 break;
104 case DataLayout::bit_data_tag:
105 break;
106 case DataLayout::no_tag:
107 case DataLayout::arg_info_data_tag:
108 return ss.as_string();
109 break;
110 default:
111 fatal(err_msg("unexpected tag %d", dp->tag()));
112 }
113 }
114 return NULL;
115 }
117 void ProfileData::print_data_on(outputStream* st, const MethodData* md) const {
118 print_data_on(st, print_data_on_helper(md));
119 }
121 #ifndef PRODUCT
122 void ProfileData::print_shared(outputStream* st, const char* name, const char* extra) const {
123 st->print("bci: %d", bci());
124 st->fill_to(tab_width_one);
125 st->print("%s", name);
126 tab(st);
127 int trap = trap_state();
128 if (trap != 0) {
129 char buf[100];
130 st->print("trap(%s) ", Deoptimization::format_trap_state(buf, sizeof(buf), trap));
131 }
132 if (extra != NULL) {
133 st->print("%s", extra);
134 }
135 int flags = data()->flags();
136 if (flags != 0) {
137 st->print("flags(%d) ", flags);
138 }
139 }
141 void ProfileData::tab(outputStream* st, bool first) const {
142 st->fill_to(first ? tab_width_one : tab_width_two);
143 }
144 #endif // !PRODUCT
146 // ==================================================================
147 // BitData
148 //
149 // A BitData corresponds to a one-bit flag. This is used to indicate
150 // whether a checkcast bytecode has seen a null value.
153 #ifndef PRODUCT
154 void BitData::print_data_on(outputStream* st, const char* extra) const {
155 print_shared(st, "BitData", extra);
156 }
157 #endif // !PRODUCT
159 // ==================================================================
160 // CounterData
161 //
162 // A CounterData corresponds to a simple counter.
164 #ifndef PRODUCT
165 void CounterData::print_data_on(outputStream* st, const char* extra) const {
166 print_shared(st, "CounterData", extra);
167 st->print_cr("count(%u)", count());
168 }
169 #endif // !PRODUCT
171 // ==================================================================
172 // JumpData
173 //
174 // A JumpData is used to access profiling information for a direct
175 // branch. It is a counter, used for counting the number of branches,
176 // plus a data displacement, used for realigning the data pointer to
177 // the corresponding target bci.
179 void JumpData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
180 assert(stream->bci() == bci(), "wrong pos");
181 int target;
182 Bytecodes::Code c = stream->code();
183 if (c == Bytecodes::_goto_w || c == Bytecodes::_jsr_w) {
184 target = stream->dest_w();
185 } else {
186 target = stream->dest();
187 }
188 int my_di = mdo->dp_to_di(dp());
189 int target_di = mdo->bci_to_di(target);
190 int offset = target_di - my_di;
191 set_displacement(offset);
192 }
194 #ifndef PRODUCT
195 void JumpData::print_data_on(outputStream* st, const char* extra) const {
196 print_shared(st, "JumpData", extra);
197 st->print_cr("taken(%u) displacement(%d)", taken(), displacement());
198 }
199 #endif // !PRODUCT
201 int TypeStackSlotEntries::compute_cell_count(Symbol* signature, bool include_receiver, int max) {
202 // Parameter profiling include the receiver
203 int args_count = include_receiver ? 1 : 0;
204 ResourceMark rm;
205 SignatureStream ss(signature);
206 args_count += ss.reference_parameter_count();
207 args_count = MIN2(args_count, max);
208 return args_count * per_arg_cell_count;
209 }
211 int TypeEntriesAtCall::compute_cell_count(BytecodeStream* stream) {
212 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
213 assert(TypeStackSlotEntries::per_arg_count() > ReturnTypeEntry::static_cell_count(), "code to test for arguments/results broken");
214 Bytecode_invoke inv(stream->method(), stream->bci());
215 int args_cell = 0;
216 if (arguments_profiling_enabled()) {
217 args_cell = TypeStackSlotEntries::compute_cell_count(inv.signature(), false, TypeProfileArgsLimit);
218 }
219 int ret_cell = 0;
220 if (return_profiling_enabled() && (inv.result_type() == T_OBJECT || inv.result_type() == T_ARRAY)) {
221 ret_cell = ReturnTypeEntry::static_cell_count();
222 }
223 int header_cell = 0;
224 if (args_cell + ret_cell > 0) {
225 header_cell = header_cell_count();
226 }
228 return header_cell + args_cell + ret_cell;
229 }
231 class ArgumentOffsetComputer : public SignatureInfo {
232 private:
233 int _max;
234 GrowableArray<int> _offsets;
236 void set(int size, BasicType type) { _size += size; }
237 void do_object(int begin, int end) {
238 if (_offsets.length() < _max) {
239 _offsets.push(_size);
240 }
241 SignatureInfo::do_object(begin, end);
242 }
243 void do_array (int begin, int end) {
244 if (_offsets.length() < _max) {
245 _offsets.push(_size);
246 }
247 SignatureInfo::do_array(begin, end);
248 }
250 public:
251 ArgumentOffsetComputer(Symbol* signature, int max)
252 : SignatureInfo(signature), _max(max), _offsets(Thread::current(), max) {
253 }
255 int total() { lazy_iterate_parameters(); return _size; }
257 int off_at(int i) const { return _offsets.at(i); }
258 };
260 void TypeStackSlotEntries::post_initialize(Symbol* signature, bool has_receiver, bool include_receiver) {
261 ResourceMark rm;
262 int start = 0;
263 // Parameter profiling include the receiver
264 if (include_receiver && has_receiver) {
265 set_stack_slot(0, 0);
266 set_type(0, type_none());
267 start += 1;
268 }
269 ArgumentOffsetComputer aos(signature, _number_of_entries-start);
270 aos.total();
271 for (int i = start; i < _number_of_entries; i++) {
272 set_stack_slot(i, aos.off_at(i-start) + (has_receiver ? 1 : 0));
273 set_type(i, type_none());
274 }
275 }
277 void CallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
278 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
279 Bytecode_invoke inv(stream->method(), stream->bci());
281 SignatureStream ss(inv.signature());
282 if (has_arguments()) {
283 #ifdef ASSERT
284 ResourceMark rm;
285 int count = MIN2(ss.reference_parameter_count(), (int)TypeProfileArgsLimit);
286 assert(count > 0, "room for args type but none found?");
287 check_number_of_arguments(count);
288 #endif
289 _args.post_initialize(inv.signature(), inv.has_receiver(), false);
290 }
292 if (has_return()) {
293 assert(inv.result_type() == T_OBJECT || inv.result_type() == T_ARRAY, "room for a ret type but doesn't return obj?");
294 _ret.post_initialize();
295 }
296 }
298 void VirtualCallTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
299 assert(Bytecodes::is_invoke(stream->code()), "should be invoke");
300 Bytecode_invoke inv(stream->method(), stream->bci());
302 if (has_arguments()) {
303 #ifdef ASSERT
304 ResourceMark rm;
305 SignatureStream ss(inv.signature());
306 int count = MIN2(ss.reference_parameter_count(), (int)TypeProfileArgsLimit);
307 assert(count > 0, "room for args type but none found?");
308 check_number_of_arguments(count);
309 #endif
310 _args.post_initialize(inv.signature(), inv.has_receiver(), false);
311 }
313 if (has_return()) {
314 assert(inv.result_type() == T_OBJECT || inv.result_type() == T_ARRAY, "room for a ret type but doesn't return obj?");
315 _ret.post_initialize();
316 }
317 }
319 bool TypeEntries::is_loader_alive(BoolObjectClosure* is_alive_cl, intptr_t p) {
320 Klass* k = (Klass*)klass_part(p);
321 return k != NULL && k->is_loader_alive(is_alive_cl);
322 }
324 void TypeStackSlotEntries::clean_weak_klass_links(BoolObjectClosure* is_alive_cl) {
325 for (int i = 0; i < _number_of_entries; i++) {
326 intptr_t p = type(i);
327 if (!is_loader_alive(is_alive_cl, p)) {
328 set_type(i, with_status((Klass*)NULL, p));
329 }
330 }
331 }
333 void ReturnTypeEntry::clean_weak_klass_links(BoolObjectClosure* is_alive_cl) {
334 intptr_t p = type();
335 if (!is_loader_alive(is_alive_cl, p)) {
336 set_type(with_status((Klass*)NULL, p));
337 }
338 }
340 bool TypeEntriesAtCall::return_profiling_enabled() {
341 return MethodData::profile_return();
342 }
344 bool TypeEntriesAtCall::arguments_profiling_enabled() {
345 return MethodData::profile_arguments();
346 }
348 #ifndef PRODUCT
349 void TypeEntries::print_klass(outputStream* st, intptr_t k) {
350 if (is_type_none(k)) {
351 st->print("none");
352 } else if (is_type_unknown(k)) {
353 st->print("unknown");
354 } else {
355 valid_klass(k)->print_value_on(st);
356 }
357 if (was_null_seen(k)) {
358 st->print(" (null seen)");
359 }
360 }
362 void TypeStackSlotEntries::print_data_on(outputStream* st) const {
363 for (int i = 0; i < _number_of_entries; i++) {
364 _pd->tab(st);
365 st->print("%d: stack(%u) ", i, stack_slot(i));
366 print_klass(st, type(i));
367 st->cr();
368 }
369 }
371 void ReturnTypeEntry::print_data_on(outputStream* st) const {
372 _pd->tab(st);
373 print_klass(st, type());
374 st->cr();
375 }
377 void CallTypeData::print_data_on(outputStream* st, const char* extra) const {
378 CounterData::print_data_on(st, extra);
379 if (has_arguments()) {
380 tab(st, true);
381 st->print("argument types");
382 _args.print_data_on(st);
383 }
384 if (has_return()) {
385 tab(st, true);
386 st->print("return type");
387 _ret.print_data_on(st);
388 }
389 }
391 void VirtualCallTypeData::print_data_on(outputStream* st, const char* extra) const {
392 VirtualCallData::print_data_on(st, extra);
393 if (has_arguments()) {
394 tab(st, true);
395 st->print("argument types");
396 _args.print_data_on(st);
397 }
398 if (has_return()) {
399 tab(st, true);
400 st->print("return type");
401 _ret.print_data_on(st);
402 }
403 }
404 #endif
406 // ==================================================================
407 // ReceiverTypeData
408 //
409 // A ReceiverTypeData is used to access profiling information about a
410 // dynamic type check. It consists of a counter which counts the total times
411 // that the check is reached, and a series of (Klass*, count) pairs
412 // which are used to store a type profile for the receiver of the check.
414 void ReceiverTypeData::clean_weak_klass_links(BoolObjectClosure* is_alive_cl) {
415 for (uint row = 0; row < row_limit(); row++) {
416 Klass* p = receiver(row);
417 if (p != NULL && !p->is_loader_alive(is_alive_cl)) {
418 clear_row(row);
419 }
420 }
421 }
423 #ifndef PRODUCT
424 void ReceiverTypeData::print_receiver_data_on(outputStream* st) const {
425 uint row;
426 int entries = 0;
427 for (row = 0; row < row_limit(); row++) {
428 if (receiver(row) != NULL) entries++;
429 }
430 st->print_cr("count(%u) entries(%u)", count(), entries);
431 int total = count();
432 for (row = 0; row < row_limit(); row++) {
433 if (receiver(row) != NULL) {
434 total += receiver_count(row);
435 }
436 }
437 for (row = 0; row < row_limit(); row++) {
438 if (receiver(row) != NULL) {
439 tab(st);
440 receiver(row)->print_value_on(st);
441 st->print_cr("(%u %4.2f)", receiver_count(row), (float) receiver_count(row) / (float) total);
442 }
443 }
444 }
445 void ReceiverTypeData::print_data_on(outputStream* st, const char* extra) const {
446 print_shared(st, "ReceiverTypeData", extra);
447 print_receiver_data_on(st);
448 }
449 void VirtualCallData::print_data_on(outputStream* st, const char* extra) const {
450 print_shared(st, "VirtualCallData", extra);
451 print_receiver_data_on(st);
452 }
453 #endif // !PRODUCT
455 // ==================================================================
456 // RetData
457 //
458 // A RetData is used to access profiling information for a ret bytecode.
459 // It is composed of a count of the number of times that the ret has
460 // been executed, followed by a series of triples of the form
461 // (bci, count, di) which count the number of times that some bci was the
462 // target of the ret and cache a corresponding displacement.
464 void RetData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
465 for (uint row = 0; row < row_limit(); row++) {
466 set_bci_displacement(row, -1);
467 set_bci(row, no_bci);
468 }
469 // release so other threads see a consistent state. bci is used as
470 // a valid flag for bci_displacement.
471 OrderAccess::release();
472 }
474 // This routine needs to atomically update the RetData structure, so the
475 // caller needs to hold the RetData_lock before it gets here. Since taking
476 // the lock can block (and allow GC) and since RetData is a ProfileData is a
477 // wrapper around a derived oop, taking the lock in _this_ method will
478 // basically cause the 'this' pointer's _data field to contain junk after the
479 // lock. We require the caller to take the lock before making the ProfileData
480 // structure. Currently the only caller is InterpreterRuntime::update_mdp_for_ret
481 address RetData::fixup_ret(int return_bci, MethodData* h_mdo) {
482 // First find the mdp which corresponds to the return bci.
483 address mdp = h_mdo->bci_to_dp(return_bci);
485 // Now check to see if any of the cache slots are open.
486 for (uint row = 0; row < row_limit(); row++) {
487 if (bci(row) == no_bci) {
488 set_bci_displacement(row, mdp - dp());
489 set_bci_count(row, DataLayout::counter_increment);
490 // Barrier to ensure displacement is written before the bci; allows
491 // the interpreter to read displacement without fear of race condition.
492 release_set_bci(row, return_bci);
493 break;
494 }
495 }
496 return mdp;
497 }
499 #ifdef CC_INTERP
500 DataLayout* RetData::advance(MethodData *md, int bci) {
501 return (DataLayout*) md->bci_to_dp(bci);
502 }
503 #endif // CC_INTERP
505 #ifndef PRODUCT
506 void RetData::print_data_on(outputStream* st, const char* extra) const {
507 print_shared(st, "RetData", extra);
508 uint row;
509 int entries = 0;
510 for (row = 0; row < row_limit(); row++) {
511 if (bci(row) != no_bci) entries++;
512 }
513 st->print_cr("count(%u) entries(%u)", count(), entries);
514 for (row = 0; row < row_limit(); row++) {
515 if (bci(row) != no_bci) {
516 tab(st);
517 st->print_cr("bci(%d: count(%u) displacement(%d))",
518 bci(row), bci_count(row), bci_displacement(row));
519 }
520 }
521 }
522 #endif // !PRODUCT
524 // ==================================================================
525 // BranchData
526 //
527 // A BranchData is used to access profiling data for a two-way branch.
528 // It consists of taken and not_taken counts as well as a data displacement
529 // for the taken case.
531 void BranchData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
532 assert(stream->bci() == bci(), "wrong pos");
533 int target = stream->dest();
534 int my_di = mdo->dp_to_di(dp());
535 int target_di = mdo->bci_to_di(target);
536 int offset = target_di - my_di;
537 set_displacement(offset);
538 }
540 #ifndef PRODUCT
541 void BranchData::print_data_on(outputStream* st, const char* extra) const {
542 print_shared(st, "BranchData", extra);
543 st->print_cr("taken(%u) displacement(%d)",
544 taken(), displacement());
545 tab(st);
546 st->print_cr("not taken(%u)", not_taken());
547 }
548 #endif
550 // ==================================================================
551 // MultiBranchData
552 //
553 // A MultiBranchData is used to access profiling information for
554 // a multi-way branch (*switch bytecodes). It consists of a series
555 // of (count, displacement) pairs, which count the number of times each
556 // case was taken and specify the data displacment for each branch target.
558 int MultiBranchData::compute_cell_count(BytecodeStream* stream) {
559 int cell_count = 0;
560 if (stream->code() == Bytecodes::_tableswitch) {
561 Bytecode_tableswitch sw(stream->method()(), stream->bcp());
562 cell_count = 1 + per_case_cell_count * (1 + sw.length()); // 1 for default
563 } else {
564 Bytecode_lookupswitch sw(stream->method()(), stream->bcp());
565 cell_count = 1 + per_case_cell_count * (sw.number_of_pairs() + 1); // 1 for default
566 }
567 return cell_count;
568 }
570 void MultiBranchData::post_initialize(BytecodeStream* stream,
571 MethodData* mdo) {
572 assert(stream->bci() == bci(), "wrong pos");
573 int target;
574 int my_di;
575 int target_di;
576 int offset;
577 if (stream->code() == Bytecodes::_tableswitch) {
578 Bytecode_tableswitch sw(stream->method()(), stream->bcp());
579 int len = sw.length();
580 assert(array_len() == per_case_cell_count * (len + 1), "wrong len");
581 for (int count = 0; count < len; count++) {
582 target = sw.dest_offset_at(count) + bci();
583 my_di = mdo->dp_to_di(dp());
584 target_di = mdo->bci_to_di(target);
585 offset = target_di - my_di;
586 set_displacement_at(count, offset);
587 }
588 target = sw.default_offset() + bci();
589 my_di = mdo->dp_to_di(dp());
590 target_di = mdo->bci_to_di(target);
591 offset = target_di - my_di;
592 set_default_displacement(offset);
594 } else {
595 Bytecode_lookupswitch sw(stream->method()(), stream->bcp());
596 int npairs = sw.number_of_pairs();
597 assert(array_len() == per_case_cell_count * (npairs + 1), "wrong len");
598 for (int count = 0; count < npairs; count++) {
599 LookupswitchPair pair = sw.pair_at(count);
600 target = pair.offset() + bci();
601 my_di = mdo->dp_to_di(dp());
602 target_di = mdo->bci_to_di(target);
603 offset = target_di - my_di;
604 set_displacement_at(count, offset);
605 }
606 target = sw.default_offset() + bci();
607 my_di = mdo->dp_to_di(dp());
608 target_di = mdo->bci_to_di(target);
609 offset = target_di - my_di;
610 set_default_displacement(offset);
611 }
612 }
614 #ifndef PRODUCT
615 void MultiBranchData::print_data_on(outputStream* st, const char* extra) const {
616 print_shared(st, "MultiBranchData", extra);
617 st->print_cr("default_count(%u) displacement(%d)",
618 default_count(), default_displacement());
619 int cases = number_of_cases();
620 for (int i = 0; i < cases; i++) {
621 tab(st);
622 st->print_cr("count(%u) displacement(%d)",
623 count_at(i), displacement_at(i));
624 }
625 }
626 #endif
628 #ifndef PRODUCT
629 void ArgInfoData::print_data_on(outputStream* st, const char* extra) const {
630 print_shared(st, "ArgInfoData", extra);
631 int nargs = number_of_args();
632 for (int i = 0; i < nargs; i++) {
633 st->print(" 0x%x", arg_modified(i));
634 }
635 st->cr();
636 }
638 #endif
640 int ParametersTypeData::compute_cell_count(Method* m) {
641 if (!MethodData::profile_parameters_for_method(m)) {
642 return 0;
643 }
644 int max = TypeProfileParmsLimit == -1 ? INT_MAX : TypeProfileParmsLimit;
645 int obj_args = TypeStackSlotEntries::compute_cell_count(m->signature(), !m->is_static(), max);
646 if (obj_args > 0) {
647 return obj_args + 1; // 1 cell for array len
648 }
649 return 0;
650 }
652 void ParametersTypeData::post_initialize(BytecodeStream* stream, MethodData* mdo) {
653 _parameters.post_initialize(mdo->method()->signature(), !mdo->method()->is_static(), true);
654 }
656 bool ParametersTypeData::profiling_enabled() {
657 return MethodData::profile_parameters();
658 }
660 #ifndef PRODUCT
661 void ParametersTypeData::print_data_on(outputStream* st, const char* extra) const {
662 st->print("parameter types"); // FIXME extra ignored?
663 _parameters.print_data_on(st);
664 }
666 void SpeculativeTrapData::print_data_on(outputStream* st, const char* extra) const {
667 print_shared(st, "SpeculativeTrapData", extra);
668 tab(st);
669 method()->print_short_name(st);
670 st->cr();
671 }
672 #endif
674 // ==================================================================
675 // MethodData*
676 //
677 // A MethodData* holds information which has been collected about
678 // a method.
680 MethodData* MethodData::allocate(ClassLoaderData* loader_data, methodHandle method, TRAPS) {
681 int size = MethodData::compute_allocation_size_in_words(method);
683 return new (loader_data, size, false, MetaspaceObj::MethodDataType, THREAD)
684 MethodData(method(), size, CHECK_NULL);
685 }
687 int MethodData::bytecode_cell_count(Bytecodes::Code code) {
688 #if defined(COMPILER1) && !defined(COMPILER2)
689 return no_profile_data;
690 #else
691 switch (code) {
692 case Bytecodes::_checkcast:
693 case Bytecodes::_instanceof:
694 case Bytecodes::_aastore:
695 if (TypeProfileCasts) {
696 return ReceiverTypeData::static_cell_count();
697 } else {
698 return BitData::static_cell_count();
699 }
700 case Bytecodes::_invokespecial:
701 case Bytecodes::_invokestatic:
702 if (MethodData::profile_arguments() || MethodData::profile_return()) {
703 return variable_cell_count;
704 } else {
705 return CounterData::static_cell_count();
706 }
707 case Bytecodes::_goto:
708 case Bytecodes::_goto_w:
709 case Bytecodes::_jsr:
710 case Bytecodes::_jsr_w:
711 return JumpData::static_cell_count();
712 case Bytecodes::_invokevirtual:
713 case Bytecodes::_invokeinterface:
714 if (MethodData::profile_arguments() || MethodData::profile_return()) {
715 return variable_cell_count;
716 } else {
717 return VirtualCallData::static_cell_count();
718 }
719 case Bytecodes::_invokedynamic:
720 if (MethodData::profile_arguments() || MethodData::profile_return()) {
721 return variable_cell_count;
722 } else {
723 return CounterData::static_cell_count();
724 }
725 case Bytecodes::_ret:
726 return RetData::static_cell_count();
727 case Bytecodes::_ifeq:
728 case Bytecodes::_ifne:
729 case Bytecodes::_iflt:
730 case Bytecodes::_ifge:
731 case Bytecodes::_ifgt:
732 case Bytecodes::_ifle:
733 case Bytecodes::_if_icmpeq:
734 case Bytecodes::_if_icmpne:
735 case Bytecodes::_if_icmplt:
736 case Bytecodes::_if_icmpge:
737 case Bytecodes::_if_icmpgt:
738 case Bytecodes::_if_icmple:
739 case Bytecodes::_if_acmpeq:
740 case Bytecodes::_if_acmpne:
741 case Bytecodes::_ifnull:
742 case Bytecodes::_ifnonnull:
743 return BranchData::static_cell_count();
744 case Bytecodes::_lookupswitch:
745 case Bytecodes::_tableswitch:
746 return variable_cell_count;
747 }
748 return no_profile_data;
749 #endif
750 }
752 // Compute the size of the profiling information corresponding to
753 // the current bytecode.
754 int MethodData::compute_data_size(BytecodeStream* stream) {
755 int cell_count = bytecode_cell_count(stream->code());
756 if (cell_count == no_profile_data) {
757 return 0;
758 }
759 if (cell_count == variable_cell_count) {
760 switch (stream->code()) {
761 case Bytecodes::_lookupswitch:
762 case Bytecodes::_tableswitch:
763 cell_count = MultiBranchData::compute_cell_count(stream);
764 break;
765 case Bytecodes::_invokespecial:
766 case Bytecodes::_invokestatic:
767 case Bytecodes::_invokedynamic:
768 assert(MethodData::profile_arguments() || MethodData::profile_return(), "should be collecting args profile");
769 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
770 profile_return_for_invoke(stream->method(), stream->bci())) {
771 cell_count = CallTypeData::compute_cell_count(stream);
772 } else {
773 cell_count = CounterData::static_cell_count();
774 }
775 break;
776 case Bytecodes::_invokevirtual:
777 case Bytecodes::_invokeinterface: {
778 assert(MethodData::profile_arguments() || MethodData::profile_return(), "should be collecting args profile");
779 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
780 profile_return_for_invoke(stream->method(), stream->bci())) {
781 cell_count = VirtualCallTypeData::compute_cell_count(stream);
782 } else {
783 cell_count = VirtualCallData::static_cell_count();
784 }
785 break;
786 }
787 default:
788 fatal("unexpected bytecode for var length profile data");
789 }
790 }
791 // Note: cell_count might be zero, meaning that there is just
792 // a DataLayout header, with no extra cells.
793 assert(cell_count >= 0, "sanity");
794 return DataLayout::compute_size_in_bytes(cell_count);
795 }
797 bool MethodData::is_speculative_trap_bytecode(Bytecodes::Code code) {
798 // Bytecodes for which we may use speculation
799 switch (code) {
800 case Bytecodes::_checkcast:
801 case Bytecodes::_instanceof:
802 case Bytecodes::_aastore:
803 case Bytecodes::_invokevirtual:
804 case Bytecodes::_invokeinterface:
805 case Bytecodes::_if_acmpeq:
806 case Bytecodes::_if_acmpne:
807 case Bytecodes::_invokestatic:
808 #ifdef COMPILER2
809 return UseTypeSpeculation;
810 #endif
811 default:
812 return false;
813 }
814 return false;
815 }
817 int MethodData::compute_extra_data_count(int data_size, int empty_bc_count, bool needs_speculative_traps) {
818 if (ProfileTraps) {
819 // Assume that up to 3% of BCIs with no MDP will need to allocate one.
820 int extra_data_count = (uint)(empty_bc_count * 3) / 128 + 1;
821 // If the method is large, let the extra BCIs grow numerous (to ~1%).
822 int one_percent_of_data
823 = (uint)data_size / (DataLayout::header_size_in_bytes()*128);
824 if (extra_data_count < one_percent_of_data)
825 extra_data_count = one_percent_of_data;
826 if (extra_data_count > empty_bc_count)
827 extra_data_count = empty_bc_count; // no need for more
829 // Make sure we have a minimum number of extra data slots to
830 // allocate SpeculativeTrapData entries. We would want to have one
831 // entry per compilation that inlines this method and for which
832 // some type speculation assumption fails. So the room we need for
833 // the SpeculativeTrapData entries doesn't directly depend on the
834 // size of the method. Because it's hard to estimate, we reserve
835 // space for an arbitrary number of entries.
836 int spec_data_count = (needs_speculative_traps ? SpecTrapLimitExtraEntries : 0) *
837 (SpeculativeTrapData::static_cell_count() + DataLayout::header_size_in_cells());
839 return MAX2(extra_data_count, spec_data_count);
840 } else {
841 return 0;
842 }
843 }
845 // Compute the size of the MethodData* necessary to store
846 // profiling information about a given method. Size is in bytes.
847 int MethodData::compute_allocation_size_in_bytes(methodHandle method) {
848 int data_size = 0;
849 BytecodeStream stream(method);
850 Bytecodes::Code c;
851 int empty_bc_count = 0; // number of bytecodes lacking data
852 bool needs_speculative_traps = false;
853 while ((c = stream.next()) >= 0) {
854 int size_in_bytes = compute_data_size(&stream);
855 data_size += size_in_bytes;
856 if (size_in_bytes == 0) empty_bc_count += 1;
857 needs_speculative_traps = needs_speculative_traps || is_speculative_trap_bytecode(c);
858 }
859 int object_size = in_bytes(data_offset()) + data_size;
861 // Add some extra DataLayout cells (at least one) to track stray traps.
862 int extra_data_count = compute_extra_data_count(data_size, empty_bc_count, needs_speculative_traps);
863 object_size += extra_data_count * DataLayout::compute_size_in_bytes(0);
865 // Add a cell to record information about modified arguments.
866 int arg_size = method->size_of_parameters();
867 object_size += DataLayout::compute_size_in_bytes(arg_size+1);
869 // Reserve room for an area of the MDO dedicated to profiling of
870 // parameters
871 int args_cell = ParametersTypeData::compute_cell_count(method());
872 if (args_cell > 0) {
873 object_size += DataLayout::compute_size_in_bytes(args_cell);
874 }
875 return object_size;
876 }
878 // Compute the size of the MethodData* necessary to store
879 // profiling information about a given method. Size is in words
880 int MethodData::compute_allocation_size_in_words(methodHandle method) {
881 int byte_size = compute_allocation_size_in_bytes(method);
882 int word_size = align_size_up(byte_size, BytesPerWord) / BytesPerWord;
883 return align_object_size(word_size);
884 }
886 // Initialize an individual data segment. Returns the size of
887 // the segment in bytes.
888 int MethodData::initialize_data(BytecodeStream* stream,
889 int data_index) {
890 #if defined(COMPILER1) && !defined(COMPILER2)
891 return 0;
892 #else
893 int cell_count = -1;
894 int tag = DataLayout::no_tag;
895 DataLayout* data_layout = data_layout_at(data_index);
896 Bytecodes::Code c = stream->code();
897 switch (c) {
898 case Bytecodes::_checkcast:
899 case Bytecodes::_instanceof:
900 case Bytecodes::_aastore:
901 if (TypeProfileCasts) {
902 cell_count = ReceiverTypeData::static_cell_count();
903 tag = DataLayout::receiver_type_data_tag;
904 } else {
905 cell_count = BitData::static_cell_count();
906 tag = DataLayout::bit_data_tag;
907 }
908 break;
909 case Bytecodes::_invokespecial:
910 case Bytecodes::_invokestatic: {
911 int counter_data_cell_count = CounterData::static_cell_count();
912 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
913 profile_return_for_invoke(stream->method(), stream->bci())) {
914 cell_count = CallTypeData::compute_cell_count(stream);
915 } else {
916 cell_count = counter_data_cell_count;
917 }
918 if (cell_count > counter_data_cell_count) {
919 tag = DataLayout::call_type_data_tag;
920 } else {
921 tag = DataLayout::counter_data_tag;
922 }
923 break;
924 }
925 case Bytecodes::_goto:
926 case Bytecodes::_goto_w:
927 case Bytecodes::_jsr:
928 case Bytecodes::_jsr_w:
929 cell_count = JumpData::static_cell_count();
930 tag = DataLayout::jump_data_tag;
931 break;
932 case Bytecodes::_invokevirtual:
933 case Bytecodes::_invokeinterface: {
934 int virtual_call_data_cell_count = VirtualCallData::static_cell_count();
935 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
936 profile_return_for_invoke(stream->method(), stream->bci())) {
937 cell_count = VirtualCallTypeData::compute_cell_count(stream);
938 } else {
939 cell_count = virtual_call_data_cell_count;
940 }
941 if (cell_count > virtual_call_data_cell_count) {
942 tag = DataLayout::virtual_call_type_data_tag;
943 } else {
944 tag = DataLayout::virtual_call_data_tag;
945 }
946 break;
947 }
948 case Bytecodes::_invokedynamic: {
949 // %%% should make a type profile for any invokedynamic that takes a ref argument
950 int counter_data_cell_count = CounterData::static_cell_count();
951 if (profile_arguments_for_invoke(stream->method(), stream->bci()) ||
952 profile_return_for_invoke(stream->method(), stream->bci())) {
953 cell_count = CallTypeData::compute_cell_count(stream);
954 } else {
955 cell_count = counter_data_cell_count;
956 }
957 if (cell_count > counter_data_cell_count) {
958 tag = DataLayout::call_type_data_tag;
959 } else {
960 tag = DataLayout::counter_data_tag;
961 }
962 break;
963 }
964 case Bytecodes::_ret:
965 cell_count = RetData::static_cell_count();
966 tag = DataLayout::ret_data_tag;
967 break;
968 case Bytecodes::_ifeq:
969 case Bytecodes::_ifne:
970 case Bytecodes::_iflt:
971 case Bytecodes::_ifge:
972 case Bytecodes::_ifgt:
973 case Bytecodes::_ifle:
974 case Bytecodes::_if_icmpeq:
975 case Bytecodes::_if_icmpne:
976 case Bytecodes::_if_icmplt:
977 case Bytecodes::_if_icmpge:
978 case Bytecodes::_if_icmpgt:
979 case Bytecodes::_if_icmple:
980 case Bytecodes::_if_acmpeq:
981 case Bytecodes::_if_acmpne:
982 case Bytecodes::_ifnull:
983 case Bytecodes::_ifnonnull:
984 cell_count = BranchData::static_cell_count();
985 tag = DataLayout::branch_data_tag;
986 break;
987 case Bytecodes::_lookupswitch:
988 case Bytecodes::_tableswitch:
989 cell_count = MultiBranchData::compute_cell_count(stream);
990 tag = DataLayout::multi_branch_data_tag;
991 break;
992 }
993 assert(tag == DataLayout::multi_branch_data_tag ||
994 ((MethodData::profile_arguments() || MethodData::profile_return()) &&
995 (tag == DataLayout::call_type_data_tag ||
996 tag == DataLayout::counter_data_tag ||
997 tag == DataLayout::virtual_call_type_data_tag ||
998 tag == DataLayout::virtual_call_data_tag)) ||
999 cell_count == bytecode_cell_count(c), "cell counts must agree");
1000 if (cell_count >= 0) {
1001 assert(tag != DataLayout::no_tag, "bad tag");
1002 assert(bytecode_has_profile(c), "agree w/ BHP");
1003 data_layout->initialize(tag, stream->bci(), cell_count);
1004 return DataLayout::compute_size_in_bytes(cell_count);
1005 } else {
1006 assert(!bytecode_has_profile(c), "agree w/ !BHP");
1007 return 0;
1008 }
1009 #endif
1010 }
1012 // Get the data at an arbitrary (sort of) data index.
1013 ProfileData* MethodData::data_at(int data_index) const {
1014 if (out_of_bounds(data_index)) {
1015 return NULL;
1016 }
1017 DataLayout* data_layout = data_layout_at(data_index);
1018 return data_layout->data_in();
1019 }
1021 ProfileData* DataLayout::data_in() {
1022 switch (tag()) {
1023 case DataLayout::no_tag:
1024 default:
1025 ShouldNotReachHere();
1026 return NULL;
1027 case DataLayout::bit_data_tag:
1028 return new BitData(this);
1029 case DataLayout::counter_data_tag:
1030 return new CounterData(this);
1031 case DataLayout::jump_data_tag:
1032 return new JumpData(this);
1033 case DataLayout::receiver_type_data_tag:
1034 return new ReceiverTypeData(this);
1035 case DataLayout::virtual_call_data_tag:
1036 return new VirtualCallData(this);
1037 case DataLayout::ret_data_tag:
1038 return new RetData(this);
1039 case DataLayout::branch_data_tag:
1040 return new BranchData(this);
1041 case DataLayout::multi_branch_data_tag:
1042 return new MultiBranchData(this);
1043 case DataLayout::arg_info_data_tag:
1044 return new ArgInfoData(this);
1045 case DataLayout::call_type_data_tag:
1046 return new CallTypeData(this);
1047 case DataLayout::virtual_call_type_data_tag:
1048 return new VirtualCallTypeData(this);
1049 case DataLayout::parameters_type_data_tag:
1050 return new ParametersTypeData(this);
1051 };
1052 }
1054 // Iteration over data.
1055 ProfileData* MethodData::next_data(ProfileData* current) const {
1056 int current_index = dp_to_di(current->dp());
1057 int next_index = current_index + current->size_in_bytes();
1058 ProfileData* next = data_at(next_index);
1059 return next;
1060 }
1062 // Give each of the data entries a chance to perform specific
1063 // data initialization.
1064 void MethodData::post_initialize(BytecodeStream* stream) {
1065 ResourceMark rm;
1066 ProfileData* data;
1067 for (data = first_data(); is_valid(data); data = next_data(data)) {
1068 stream->set_start(data->bci());
1069 stream->next();
1070 data->post_initialize(stream, this);
1071 }
1072 if (_parameters_type_data_di != -1) {
1073 parameters_type_data()->post_initialize(NULL, this);
1074 }
1075 }
1077 // Initialize the MethodData* corresponding to a given method.
1078 MethodData::MethodData(methodHandle method, int size, TRAPS)
1079 : _extra_data_lock(Monitor::leaf, "MDO extra data lock") {
1080 No_Safepoint_Verifier no_safepoint; // init function atomic wrt GC
1081 ResourceMark rm;
1082 // Set the method back-pointer.
1083 _method = method();
1085 init();
1086 set_creation_mileage(mileage_of(method()));
1088 // Go through the bytecodes and allocate and initialize the
1089 // corresponding data cells.
1090 int data_size = 0;
1091 int empty_bc_count = 0; // number of bytecodes lacking data
1092 _data[0] = 0; // apparently not set below.
1093 BytecodeStream stream(method);
1094 Bytecodes::Code c;
1095 bool needs_speculative_traps = false;
1096 while ((c = stream.next()) >= 0) {
1097 int size_in_bytes = initialize_data(&stream, data_size);
1098 data_size += size_in_bytes;
1099 if (size_in_bytes == 0) empty_bc_count += 1;
1100 needs_speculative_traps = needs_speculative_traps || is_speculative_trap_bytecode(c);
1101 }
1102 _data_size = data_size;
1103 int object_size = in_bytes(data_offset()) + data_size;
1105 // Add some extra DataLayout cells (at least one) to track stray traps.
1106 int extra_data_count = compute_extra_data_count(data_size, empty_bc_count, needs_speculative_traps);
1107 int extra_size = extra_data_count * DataLayout::compute_size_in_bytes(0);
1109 // Let's zero the space for the extra data
1110 Copy::zero_to_bytes(((address)_data) + data_size, extra_size);
1112 // Add a cell to record information about modified arguments.
1113 // Set up _args_modified array after traps cells so that
1114 // the code for traps cells works.
1115 DataLayout *dp = data_layout_at(data_size + extra_size);
1117 int arg_size = method->size_of_parameters();
1118 dp->initialize(DataLayout::arg_info_data_tag, 0, arg_size+1);
1120 int arg_data_size = DataLayout::compute_size_in_bytes(arg_size+1);
1121 object_size += extra_size + arg_data_size;
1123 int parms_cell = ParametersTypeData::compute_cell_count(method());
1124 // If we are profiling parameters, we reserver an area near the end
1125 // of the MDO after the slots for bytecodes (because there's no bci
1126 // for method entry so they don't fit with the framework for the
1127 // profiling of bytecodes). We store the offset within the MDO of
1128 // this area (or -1 if no parameter is profiled)
1129 if (parms_cell > 0) {
1130 object_size += DataLayout::compute_size_in_bytes(parms_cell);
1131 _parameters_type_data_di = data_size + extra_size + arg_data_size;
1132 DataLayout *dp = data_layout_at(data_size + extra_size + arg_data_size);
1133 dp->initialize(DataLayout::parameters_type_data_tag, 0, parms_cell);
1134 } else {
1135 _parameters_type_data_di = -1;
1136 }
1138 // Set an initial hint. Don't use set_hint_di() because
1139 // first_di() may be out of bounds if data_size is 0.
1140 // In that situation, _hint_di is never used, but at
1141 // least well-defined.
1142 _hint_di = first_di();
1144 post_initialize(&stream);
1146 set_size(object_size);
1147 }
1149 void MethodData::init() {
1150 _invocation_counter.init();
1151 _backedge_counter.init();
1152 _invocation_counter_start = 0;
1153 _backedge_counter_start = 0;
1154 _num_loops = 0;
1155 _num_blocks = 0;
1156 _highest_comp_level = 0;
1157 _highest_osr_comp_level = 0;
1158 _would_profile = true;
1160 #if INCLUDE_RTM_OPT
1161 _rtm_state = NoRTM; // No RTM lock eliding by default
1162 if (UseRTMLocking &&
1163 !CompilerOracle::has_option_string(_method, "NoRTMLockEliding")) {
1164 if (CompilerOracle::has_option_string(_method, "UseRTMLockEliding") || !UseRTMDeopt) {
1165 // Generate RTM lock eliding code without abort ratio calculation code.
1166 _rtm_state = UseRTM;
1167 } else if (UseRTMDeopt) {
1168 // Generate RTM lock eliding code and include abort ratio calculation
1169 // code if UseRTMDeopt is on.
1170 _rtm_state = ProfileRTM;
1171 }
1172 }
1173 #endif
1175 // Initialize flags and trap history.
1176 _nof_decompiles = 0;
1177 _nof_overflow_recompiles = 0;
1178 _nof_overflow_traps = 0;
1179 clear_escape_info();
1180 assert(sizeof(_trap_hist) % sizeof(HeapWord) == 0, "align");
1181 Copy::zero_to_words((HeapWord*) &_trap_hist,
1182 sizeof(_trap_hist) / sizeof(HeapWord));
1183 }
1185 // Get a measure of how much mileage the method has on it.
1186 int MethodData::mileage_of(Method* method) {
1187 int mileage = 0;
1188 if (TieredCompilation) {
1189 mileage = MAX2(method->invocation_count(), method->backedge_count());
1190 } else {
1191 int iic = method->interpreter_invocation_count();
1192 if (mileage < iic) mileage = iic;
1193 MethodCounters* mcs = method->method_counters();
1194 if (mcs != NULL) {
1195 InvocationCounter* ic = mcs->invocation_counter();
1196 InvocationCounter* bc = mcs->backedge_counter();
1197 int icval = ic->count();
1198 if (ic->carry()) icval += CompileThreshold;
1199 if (mileage < icval) mileage = icval;
1200 int bcval = bc->count();
1201 if (bc->carry()) bcval += CompileThreshold;
1202 if (mileage < bcval) mileage = bcval;
1203 }
1204 }
1205 return mileage;
1206 }
1208 bool MethodData::is_mature() const {
1209 return CompilationPolicy::policy()->is_mature(_method);
1210 }
1212 // Translate a bci to its corresponding data index (di).
1213 address MethodData::bci_to_dp(int bci) {
1214 ResourceMark rm;
1215 ProfileData* data = data_before(bci);
1216 ProfileData* prev = NULL;
1217 for ( ; is_valid(data); data = next_data(data)) {
1218 if (data->bci() >= bci) {
1219 if (data->bci() == bci) set_hint_di(dp_to_di(data->dp()));
1220 else if (prev != NULL) set_hint_di(dp_to_di(prev->dp()));
1221 return data->dp();
1222 }
1223 prev = data;
1224 }
1225 return (address)limit_data_position();
1226 }
1228 // Translate a bci to its corresponding data, or NULL.
1229 ProfileData* MethodData::bci_to_data(int bci) {
1230 ProfileData* data = data_before(bci);
1231 for ( ; is_valid(data); data = next_data(data)) {
1232 if (data->bci() == bci) {
1233 set_hint_di(dp_to_di(data->dp()));
1234 return data;
1235 } else if (data->bci() > bci) {
1236 break;
1237 }
1238 }
1239 return bci_to_extra_data(bci, NULL, false);
1240 }
1242 DataLayout* MethodData::next_extra(DataLayout* dp) {
1243 int nb_cells = 0;
1244 switch(dp->tag()) {
1245 case DataLayout::bit_data_tag:
1246 case DataLayout::no_tag:
1247 nb_cells = BitData::static_cell_count();
1248 break;
1249 case DataLayout::speculative_trap_data_tag:
1250 nb_cells = SpeculativeTrapData::static_cell_count();
1251 break;
1252 default:
1253 fatal(err_msg("unexpected tag %d", dp->tag()));
1254 }
1255 return (DataLayout*)((address)dp + DataLayout::compute_size_in_bytes(nb_cells));
1256 }
1258 ProfileData* MethodData::bci_to_extra_data_helper(int bci, Method* m, DataLayout*& dp, bool concurrent) {
1259 DataLayout* end = extra_data_limit();
1261 for (;; dp = next_extra(dp)) {
1262 assert(dp < end, "moved past end of extra data");
1263 // No need for "OrderAccess::load_acquire" ops,
1264 // since the data structure is monotonic.
1265 switch(dp->tag()) {
1266 case DataLayout::no_tag:
1267 return NULL;
1268 case DataLayout::arg_info_data_tag:
1269 dp = end;
1270 return NULL; // ArgInfoData is at the end of extra data section.
1271 case DataLayout::bit_data_tag:
1272 if (m == NULL && dp->bci() == bci) {
1273 return new BitData(dp);
1274 }
1275 break;
1276 case DataLayout::speculative_trap_data_tag:
1277 if (m != NULL) {
1278 SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1279 // data->method() may be null in case of a concurrent
1280 // allocation. Maybe it's for the same method. Try to use that
1281 // entry in that case.
1282 if (dp->bci() == bci) {
1283 if (data->method() == NULL) {
1284 assert(concurrent, "impossible because no concurrent allocation");
1285 return NULL;
1286 } else if (data->method() == m) {
1287 return data;
1288 }
1289 }
1290 }
1291 break;
1292 default:
1293 fatal(err_msg("unexpected tag %d", dp->tag()));
1294 }
1295 }
1296 return NULL;
1297 }
1300 // Translate a bci to its corresponding extra data, or NULL.
1301 ProfileData* MethodData::bci_to_extra_data(int bci, Method* m, bool create_if_missing) {
1302 // This code assumes an entry for a SpeculativeTrapData is 2 cells
1303 assert(2*DataLayout::compute_size_in_bytes(BitData::static_cell_count()) ==
1304 DataLayout::compute_size_in_bytes(SpeculativeTrapData::static_cell_count()),
1305 "code needs to be adjusted");
1307 DataLayout* dp = extra_data_base();
1308 DataLayout* end = extra_data_limit();
1310 // Allocation in the extra data space has to be atomic because not
1311 // all entries have the same size and non atomic concurrent
1312 // allocation would result in a corrupted extra data space.
1313 ProfileData* result = bci_to_extra_data_helper(bci, m, dp, true);
1314 if (result != NULL) {
1315 return result;
1316 }
1318 if (create_if_missing && dp < end) {
1319 MutexLocker ml(&_extra_data_lock);
1320 // Check again now that we have the lock. Another thread may
1321 // have added extra data entries.
1322 ProfileData* result = bci_to_extra_data_helper(bci, m, dp, false);
1323 if (result != NULL || dp >= end) {
1324 return result;
1325 }
1327 assert(dp->tag() == DataLayout::no_tag || (dp->tag() == DataLayout::speculative_trap_data_tag && m != NULL), "should be free");
1328 assert(next_extra(dp)->tag() == DataLayout::no_tag || next_extra(dp)->tag() == DataLayout::arg_info_data_tag, "should be free or arg info");
1329 u1 tag = m == NULL ? DataLayout::bit_data_tag : DataLayout::speculative_trap_data_tag;
1330 // SpeculativeTrapData is 2 slots. Make sure we have room.
1331 if (m != NULL && next_extra(dp)->tag() != DataLayout::no_tag) {
1332 return NULL;
1333 }
1334 DataLayout temp;
1335 temp.initialize(tag, bci, 0);
1337 dp->set_header(temp.header());
1338 assert(dp->tag() == tag, "sane");
1339 assert(dp->bci() == bci, "no concurrent allocation");
1340 if (tag == DataLayout::bit_data_tag) {
1341 return new BitData(dp);
1342 } else {
1343 SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1344 data->set_method(m);
1345 return data;
1346 }
1347 }
1348 return NULL;
1349 }
1351 ArgInfoData *MethodData::arg_info() {
1352 DataLayout* dp = extra_data_base();
1353 DataLayout* end = extra_data_limit();
1354 for (; dp < end; dp = next_extra(dp)) {
1355 if (dp->tag() == DataLayout::arg_info_data_tag)
1356 return new ArgInfoData(dp);
1357 }
1358 return NULL;
1359 }
1361 // Printing
1363 #ifndef PRODUCT
1365 void MethodData::print_on(outputStream* st) const {
1366 assert(is_methodData(), "should be method data");
1367 st->print("method data for ");
1368 method()->print_value_on(st);
1369 st->cr();
1370 print_data_on(st);
1371 }
1373 #endif //PRODUCT
1375 void MethodData::print_value_on(outputStream* st) const {
1376 assert(is_methodData(), "should be method data");
1377 st->print("method data for ");
1378 method()->print_value_on(st);
1379 }
1381 #ifndef PRODUCT
1382 void MethodData::print_data_on(outputStream* st) const {
1383 ResourceMark rm;
1384 ProfileData* data = first_data();
1385 if (_parameters_type_data_di != -1) {
1386 parameters_type_data()->print_data_on(st);
1387 }
1388 for ( ; is_valid(data); data = next_data(data)) {
1389 st->print("%d", dp_to_di(data->dp()));
1390 st->fill_to(6);
1391 data->print_data_on(st, this);
1392 }
1393 st->print_cr("--- Extra data:");
1394 DataLayout* dp = extra_data_base();
1395 DataLayout* end = extra_data_limit();
1396 for (;; dp = next_extra(dp)) {
1397 assert(dp < end, "moved past end of extra data");
1398 // No need for "OrderAccess::load_acquire" ops,
1399 // since the data structure is monotonic.
1400 switch(dp->tag()) {
1401 case DataLayout::no_tag:
1402 continue;
1403 case DataLayout::bit_data_tag:
1404 data = new BitData(dp);
1405 break;
1406 case DataLayout::speculative_trap_data_tag:
1407 data = new SpeculativeTrapData(dp);
1408 break;
1409 case DataLayout::arg_info_data_tag:
1410 data = new ArgInfoData(dp);
1411 dp = end; // ArgInfoData is at the end of extra data section.
1412 break;
1413 default:
1414 fatal(err_msg("unexpected tag %d", dp->tag()));
1415 }
1416 st->print("%d", dp_to_di(data->dp()));
1417 st->fill_to(6);
1418 data->print_data_on(st);
1419 if (dp >= end) return;
1420 }
1421 }
1422 #endif
1424 #if INCLUDE_SERVICES
1425 // Size Statistics
1426 void MethodData::collect_statistics(KlassSizeStats *sz) const {
1427 int n = sz->count(this);
1428 sz->_method_data_bytes += n;
1429 sz->_method_all_bytes += n;
1430 sz->_rw_bytes += n;
1431 }
1432 #endif // INCLUDE_SERVICES
1434 // Verification
1436 void MethodData::verify_on(outputStream* st) {
1437 guarantee(is_methodData(), "object must be method data");
1438 // guarantee(m->is_perm(), "should be in permspace");
1439 this->verify_data_on(st);
1440 }
1442 void MethodData::verify_data_on(outputStream* st) {
1443 NEEDS_CLEANUP;
1444 // not yet implemented.
1445 }
1447 bool MethodData::profile_jsr292(methodHandle m, int bci) {
1448 if (m->is_compiled_lambda_form()) {
1449 return true;
1450 }
1452 Bytecode_invoke inv(m , bci);
1453 return inv.is_invokedynamic() || inv.is_invokehandle();
1454 }
1456 int MethodData::profile_arguments_flag() {
1457 return TypeProfileLevel % 10;
1458 }
1460 bool MethodData::profile_arguments() {
1461 return profile_arguments_flag() > no_type_profile && profile_arguments_flag() <= type_profile_all;
1462 }
1464 bool MethodData::profile_arguments_jsr292_only() {
1465 return profile_arguments_flag() == type_profile_jsr292;
1466 }
1468 bool MethodData::profile_all_arguments() {
1469 return profile_arguments_flag() == type_profile_all;
1470 }
1472 bool MethodData::profile_arguments_for_invoke(methodHandle m, int bci) {
1473 if (!profile_arguments()) {
1474 return false;
1475 }
1477 if (profile_all_arguments()) {
1478 return true;
1479 }
1481 assert(profile_arguments_jsr292_only(), "inconsistent");
1482 return profile_jsr292(m, bci);
1483 }
1485 int MethodData::profile_return_flag() {
1486 return (TypeProfileLevel % 100) / 10;
1487 }
1489 bool MethodData::profile_return() {
1490 return profile_return_flag() > no_type_profile && profile_return_flag() <= type_profile_all;
1491 }
1493 bool MethodData::profile_return_jsr292_only() {
1494 return profile_return_flag() == type_profile_jsr292;
1495 }
1497 bool MethodData::profile_all_return() {
1498 return profile_return_flag() == type_profile_all;
1499 }
1501 bool MethodData::profile_return_for_invoke(methodHandle m, int bci) {
1502 if (!profile_return()) {
1503 return false;
1504 }
1506 if (profile_all_return()) {
1507 return true;
1508 }
1510 assert(profile_return_jsr292_only(), "inconsistent");
1511 return profile_jsr292(m, bci);
1512 }
1514 int MethodData::profile_parameters_flag() {
1515 return TypeProfileLevel / 100;
1516 }
1518 bool MethodData::profile_parameters() {
1519 return profile_parameters_flag() > no_type_profile && profile_parameters_flag() <= type_profile_all;
1520 }
1522 bool MethodData::profile_parameters_jsr292_only() {
1523 return profile_parameters_flag() == type_profile_jsr292;
1524 }
1526 bool MethodData::profile_all_parameters() {
1527 return profile_parameters_flag() == type_profile_all;
1528 }
1530 bool MethodData::profile_parameters_for_method(methodHandle m) {
1531 if (!profile_parameters()) {
1532 return false;
1533 }
1535 if (profile_all_parameters()) {
1536 return true;
1537 }
1539 assert(profile_parameters_jsr292_only(), "inconsistent");
1540 return m->is_compiled_lambda_form();
1541 }
1543 void MethodData::clean_extra_data_helper(DataLayout* dp, int shift, bool reset) {
1544 if (shift == 0) {
1545 return;
1546 }
1547 if (!reset) {
1548 // Move all cells of trap entry at dp left by "shift" cells
1549 intptr_t* start = (intptr_t*)dp;
1550 intptr_t* end = (intptr_t*)next_extra(dp);
1551 for (intptr_t* ptr = start; ptr < end; ptr++) {
1552 *(ptr-shift) = *ptr;
1553 }
1554 } else {
1555 // Reset "shift" cells stopping at dp
1556 intptr_t* start = ((intptr_t*)dp) - shift;
1557 intptr_t* end = (intptr_t*)dp;
1558 for (intptr_t* ptr = start; ptr < end; ptr++) {
1559 *ptr = 0;
1560 }
1561 }
1562 }
1564 // Remove SpeculativeTrapData entries that reference an unloaded
1565 // method
1566 void MethodData::clean_extra_data(BoolObjectClosure* is_alive) {
1567 DataLayout* dp = extra_data_base();
1568 DataLayout* end = extra_data_limit();
1570 int shift = 0;
1571 for (; dp < end; dp = next_extra(dp)) {
1572 switch(dp->tag()) {
1573 case DataLayout::speculative_trap_data_tag: {
1574 SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1575 Method* m = data->method();
1576 assert(m != NULL, "should have a method");
1577 if (!m->method_holder()->is_loader_alive(is_alive)) {
1578 // "shift" accumulates the number of cells for dead
1579 // SpeculativeTrapData entries that have been seen so
1580 // far. Following entries must be shifted left by that many
1581 // cells to remove the dead SpeculativeTrapData entries.
1582 shift += (int)((intptr_t*)next_extra(dp) - (intptr_t*)dp);
1583 } else {
1584 // Shift this entry left if it follows dead
1585 // SpeculativeTrapData entries
1586 clean_extra_data_helper(dp, shift);
1587 }
1588 break;
1589 }
1590 case DataLayout::bit_data_tag:
1591 // Shift this entry left if it follows dead SpeculativeTrapData
1592 // entries
1593 clean_extra_data_helper(dp, shift);
1594 continue;
1595 case DataLayout::no_tag:
1596 case DataLayout::arg_info_data_tag:
1597 // We are at end of the live trap entries. The previous "shift"
1598 // cells contain entries that are either dead or were shifted
1599 // left. They need to be reset to no_tag
1600 clean_extra_data_helper(dp, shift, true);
1601 return;
1602 default:
1603 fatal(err_msg("unexpected tag %d", dp->tag()));
1604 }
1605 }
1606 }
1608 // Verify there's no unloaded method referenced by a
1609 // SpeculativeTrapData entry
1610 void MethodData::verify_extra_data_clean(BoolObjectClosure* is_alive) {
1611 #ifdef ASSERT
1612 DataLayout* dp = extra_data_base();
1613 DataLayout* end = extra_data_limit();
1615 for (; dp < end; dp = next_extra(dp)) {
1616 switch(dp->tag()) {
1617 case DataLayout::speculative_trap_data_tag: {
1618 SpeculativeTrapData* data = new SpeculativeTrapData(dp);
1619 Method* m = data->method();
1620 assert(m != NULL && m->method_holder()->is_loader_alive(is_alive), "Method should exist");
1621 break;
1622 }
1623 case DataLayout::bit_data_tag:
1624 continue;
1625 case DataLayout::no_tag:
1626 case DataLayout::arg_info_data_tag:
1627 return;
1628 default:
1629 fatal(err_msg("unexpected tag %d", dp->tag()));
1630 }
1631 }
1632 #endif
1633 }
1635 void MethodData::clean_method_data(BoolObjectClosure* is_alive) {
1636 for (ProfileData* data = first_data();
1637 is_valid(data);
1638 data = next_data(data)) {
1639 data->clean_weak_klass_links(is_alive);
1640 }
1641 ParametersTypeData* parameters = parameters_type_data();
1642 if (parameters != NULL) {
1643 parameters->clean_weak_klass_links(is_alive);
1644 }
1646 clean_extra_data(is_alive);
1647 verify_extra_data_clean(is_alive);
1648 }