comparison src/share/vm/oops/methodDataOop.hpp @ 0:a61af66fc99e

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 48a3fa21394b
comparison
equal deleted inserted replaced
-1:000000000000 0:8ae9353512f1
1 /*
2 * Copyright 2000-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 class BytecodeStream;
26
27 // The MethodData object collects counts and other profile information
28 // during zeroth-tier (interpretive) and first-tier execution.
29 // The profile is used later by compilation heuristics. Some heuristics
30 // enable use of aggressive (or "heroic") optimizations. An aggressive
31 // optimization often has a down-side, a corner case that it handles
32 // poorly, but which is thought to be rare. The profile provides
33 // evidence of this rarity for a given method or even BCI. It allows
34 // the compiler to back out of the optimization at places where it
35 // has historically been a poor choice. Other heuristics try to use
36 // specific information gathered about types observed at a given site.
37 //
38 // All data in the profile is approximate. It is expected to be accurate
39 // on the whole, but the system expects occasional inaccuraces, due to
40 // counter overflow, multiprocessor races during data collection, space
41 // limitations, missing MDO blocks, etc. Bad or missing data will degrade
42 // optimization quality but will not affect correctness. Also, each MDO
43 // is marked with its birth-date ("creation_mileage") which can be used
44 // to assess the quality ("maturity") of its data.
45 //
46 // Short (<32-bit) counters are designed to overflow to a known "saturated"
47 // state. Also, certain recorded per-BCI events are given one-bit counters
48 // which overflow to a saturated state which applied to all counters at
49 // that BCI. In other words, there is a small lattice which approximates
50 // the ideal of an infinite-precision counter for each event at each BCI,
51 // and the lattice quickly "bottoms out" in a state where all counters
52 // are taken to be indefinitely large.
53 //
54 // The reader will find many data races in profile gathering code, starting
55 // with invocation counter incrementation. None of these races harm correct
56 // execution of the compiled code.
57
58 // DataLayout
59 //
60 // Overlay for generic profiling data.
61 class DataLayout VALUE_OBJ_CLASS_SPEC {
62 private:
63 // Every data layout begins with a header. This header
64 // contains a tag, which is used to indicate the size/layout
65 // of the data, 4 bits of flags, which can be used in any way,
66 // 4 bits of trap history (none/one reason/many reasons),
67 // and a bci, which is used to tie this piece of data to a
68 // specific bci in the bytecodes.
69 union {
70 intptr_t _bits;
71 struct {
72 u1 _tag;
73 u1 _flags;
74 u2 _bci;
75 } _struct;
76 } _header;
77
78 // The data layout has an arbitrary number of cells, each sized
79 // to accomodate a pointer or an integer.
80 intptr_t _cells[1];
81
82 // Some types of data layouts need a length field.
83 static bool needs_array_len(u1 tag);
84
85 public:
86 enum {
87 counter_increment = 1
88 };
89
90 enum {
91 cell_size = sizeof(intptr_t)
92 };
93
94 // Tag values
95 enum {
96 no_tag,
97 bit_data_tag,
98 counter_data_tag,
99 jump_data_tag,
100 receiver_type_data_tag,
101 virtual_call_data_tag,
102 ret_data_tag,
103 branch_data_tag,
104 multi_branch_data_tag
105 };
106
107 enum {
108 // The _struct._flags word is formatted as [trap_state:4 | flags:4].
109 // The trap state breaks down further as [recompile:1 | reason:3].
110 // This further breakdown is defined in deoptimization.cpp.
111 // See Deoptimization::trap_state_reason for an assert that
112 // trap_bits is big enough to hold reasons < Reason_RECORDED_LIMIT.
113 //
114 // The trap_state is collected only if ProfileTraps is true.
115 trap_bits = 1+3, // 3: enough to distinguish [0..Reason_RECORDED_LIMIT].
116 trap_shift = BitsPerByte - trap_bits,
117 trap_mask = right_n_bits(trap_bits),
118 trap_mask_in_place = (trap_mask << trap_shift),
119 flag_limit = trap_shift,
120 flag_mask = right_n_bits(flag_limit),
121 first_flag = 0
122 };
123
124 // Size computation
125 static int header_size_in_bytes() {
126 return cell_size;
127 }
128 static int header_size_in_cells() {
129 return 1;
130 }
131
132 static int compute_size_in_bytes(int cell_count) {
133 return header_size_in_bytes() + cell_count * cell_size;
134 }
135
136 // Initialization
137 void initialize(u1 tag, u2 bci, int cell_count);
138
139 // Accessors
140 u1 tag() {
141 return _header._struct._tag;
142 }
143
144 // Return a few bits of trap state. Range is [0..trap_mask].
145 // The state tells if traps with zero, one, or many reasons have occurred.
146 // It also tells whether zero or many recompilations have occurred.
147 // The associated trap histogram in the MDO itself tells whether
148 // traps are common or not. If a BCI shows that a trap X has
149 // occurred, and the MDO shows N occurrences of X, we make the
150 // simplifying assumption that all N occurrences can be blamed
151 // on that BCI.
152 int trap_state() {
153 return ((_header._struct._flags >> trap_shift) & trap_mask);
154 }
155
156 void set_trap_state(int new_state) {
157 assert(ProfileTraps, "used only under +ProfileTraps");
158 uint old_flags = (_header._struct._flags & flag_mask);
159 _header._struct._flags = (new_state << trap_shift) | old_flags;
160 assert(trap_state() == new_state, "sanity");
161 }
162
163 u1 flags() {
164 return _header._struct._flags;
165 }
166
167 u2 bci() {
168 return _header._struct._bci;
169 }
170
171 void set_header(intptr_t value) {
172 _header._bits = value;
173 }
174 void release_set_header(intptr_t value) {
175 OrderAccess::release_store_ptr(&_header._bits, value);
176 }
177 intptr_t header() {
178 return _header._bits;
179 }
180 void set_cell_at(int index, intptr_t value) {
181 _cells[index] = value;
182 }
183 void release_set_cell_at(int index, intptr_t value) {
184 OrderAccess::release_store_ptr(&_cells[index], value);
185 }
186 intptr_t cell_at(int index) {
187 return _cells[index];
188 }
189 intptr_t* adr_cell_at(int index) {
190 return &_cells[index];
191 }
192 oop* adr_oop_at(int index) {
193 return (oop*)&(_cells[index]);
194 }
195
196 void set_flag_at(int flag_number) {
197 assert(flag_number < flag_limit, "oob");
198 _header._struct._flags |= (0x1 << flag_number);
199 }
200 bool flag_at(int flag_number) {
201 assert(flag_number < flag_limit, "oob");
202 return (_header._struct._flags & (0x1 << flag_number)) != 0;
203 }
204
205 // Low-level support for code generation.
206 static ByteSize header_offset() {
207 return byte_offset_of(DataLayout, _header);
208 }
209 static ByteSize tag_offset() {
210 return byte_offset_of(DataLayout, _header._struct._tag);
211 }
212 static ByteSize flags_offset() {
213 return byte_offset_of(DataLayout, _header._struct._flags);
214 }
215 static ByteSize bci_offset() {
216 return byte_offset_of(DataLayout, _header._struct._bci);
217 }
218 static ByteSize cell_offset(int index) {
219 return byte_offset_of(DataLayout, _cells[index]);
220 }
221 // Return a value which, when or-ed as a byte into _flags, sets the flag.
222 static int flag_number_to_byte_constant(int flag_number) {
223 assert(0 <= flag_number && flag_number < flag_limit, "oob");
224 DataLayout temp; temp.set_header(0);
225 temp.set_flag_at(flag_number);
226 return temp._header._struct._flags;
227 }
228 // Return a value which, when or-ed as a word into _header, sets the flag.
229 static intptr_t flag_mask_to_header_mask(int byte_constant) {
230 DataLayout temp; temp.set_header(0);
231 temp._header._struct._flags = byte_constant;
232 return temp._header._bits;
233 }
234 };
235
236
237 // ProfileData class hierarchy
238 class ProfileData;
239 class BitData;
240 class CounterData;
241 class ReceiverTypeData;
242 class VirtualCallData;
243 class RetData;
244 class JumpData;
245 class BranchData;
246 class ArrayData;
247 class MultiBranchData;
248
249
250 // ProfileData
251 //
252 // A ProfileData object is created to refer to a section of profiling
253 // data in a structured way.
254 class ProfileData : public ResourceObj {
255 private:
256 #ifndef PRODUCT
257 enum {
258 tab_width_one = 16,
259 tab_width_two = 36
260 };
261 #endif // !PRODUCT
262
263 // This is a pointer to a section of profiling data.
264 DataLayout* _data;
265
266 protected:
267 DataLayout* data() { return _data; }
268
269 enum {
270 cell_size = DataLayout::cell_size
271 };
272
273 public:
274 // How many cells are in this?
275 virtual int cell_count() {
276 ShouldNotReachHere();
277 return -1;
278 }
279
280 // Return the size of this data.
281 int size_in_bytes() {
282 return DataLayout::compute_size_in_bytes(cell_count());
283 }
284
285 protected:
286 // Low-level accessors for underlying data
287 void set_intptr_at(int index, intptr_t value) {
288 assert(0 <= index && index < cell_count(), "oob");
289 data()->set_cell_at(index, value);
290 }
291 void release_set_intptr_at(int index, intptr_t value) {
292 assert(0 <= index && index < cell_count(), "oob");
293 data()->release_set_cell_at(index, value);
294 }
295 intptr_t intptr_at(int index) {
296 assert(0 <= index && index < cell_count(), "oob");
297 return data()->cell_at(index);
298 }
299 void set_uint_at(int index, uint value) {
300 set_intptr_at(index, (intptr_t) value);
301 }
302 void release_set_uint_at(int index, uint value) {
303 release_set_intptr_at(index, (intptr_t) value);
304 }
305 uint uint_at(int index) {
306 return (uint)intptr_at(index);
307 }
308 void set_int_at(int index, int value) {
309 set_intptr_at(index, (intptr_t) value);
310 }
311 void release_set_int_at(int index, int value) {
312 release_set_intptr_at(index, (intptr_t) value);
313 }
314 int int_at(int index) {
315 return (int)intptr_at(index);
316 }
317 int int_at_unchecked(int index) {
318 return (int)data()->cell_at(index);
319 }
320 void set_oop_at(int index, oop value) {
321 set_intptr_at(index, (intptr_t) value);
322 }
323 oop oop_at(int index) {
324 return (oop)intptr_at(index);
325 }
326 oop* adr_oop_at(int index) {
327 assert(0 <= index && index < cell_count(), "oob");
328 return data()->adr_oop_at(index);
329 }
330
331 void set_flag_at(int flag_number) {
332 data()->set_flag_at(flag_number);
333 }
334 bool flag_at(int flag_number) {
335 return data()->flag_at(flag_number);
336 }
337
338 // two convenient imports for use by subclasses:
339 static ByteSize cell_offset(int index) {
340 return DataLayout::cell_offset(index);
341 }
342 static int flag_number_to_byte_constant(int flag_number) {
343 return DataLayout::flag_number_to_byte_constant(flag_number);
344 }
345
346 ProfileData(DataLayout* data) {
347 _data = data;
348 }
349
350 public:
351 // Constructor for invalid ProfileData.
352 ProfileData();
353
354 u2 bci() {
355 return data()->bci();
356 }
357
358 address dp() {
359 return (address)_data;
360 }
361
362 int trap_state() {
363 return data()->trap_state();
364 }
365 void set_trap_state(int new_state) {
366 data()->set_trap_state(new_state);
367 }
368
369 // Type checking
370 virtual bool is_BitData() { return false; }
371 virtual bool is_CounterData() { return false; }
372 virtual bool is_JumpData() { return false; }
373 virtual bool is_ReceiverTypeData(){ return false; }
374 virtual bool is_VirtualCallData() { return false; }
375 virtual bool is_RetData() { return false; }
376 virtual bool is_BranchData() { return false; }
377 virtual bool is_ArrayData() { return false; }
378 virtual bool is_MultiBranchData() { return false; }
379
380 BitData* as_BitData() {
381 assert(is_BitData(), "wrong type");
382 return is_BitData() ? (BitData*) this : NULL;
383 }
384 CounterData* as_CounterData() {
385 assert(is_CounterData(), "wrong type");
386 return is_CounterData() ? (CounterData*) this : NULL;
387 }
388 JumpData* as_JumpData() {
389 assert(is_JumpData(), "wrong type");
390 return is_JumpData() ? (JumpData*) this : NULL;
391 }
392 ReceiverTypeData* as_ReceiverTypeData() {
393 assert(is_ReceiverTypeData(), "wrong type");
394 return is_ReceiverTypeData() ? (ReceiverTypeData*)this : NULL;
395 }
396 VirtualCallData* as_VirtualCallData() {
397 assert(is_VirtualCallData(), "wrong type");
398 return is_VirtualCallData() ? (VirtualCallData*)this : NULL;
399 }
400 RetData* as_RetData() {
401 assert(is_RetData(), "wrong type");
402 return is_RetData() ? (RetData*) this : NULL;
403 }
404 BranchData* as_BranchData() {
405 assert(is_BranchData(), "wrong type");
406 return is_BranchData() ? (BranchData*) this : NULL;
407 }
408 ArrayData* as_ArrayData() {
409 assert(is_ArrayData(), "wrong type");
410 return is_ArrayData() ? (ArrayData*) this : NULL;
411 }
412 MultiBranchData* as_MultiBranchData() {
413 assert(is_MultiBranchData(), "wrong type");
414 return is_MultiBranchData() ? (MultiBranchData*)this : NULL;
415 }
416
417
418 // Subclass specific initialization
419 virtual void post_initialize(BytecodeStream* stream, methodDataOop mdo) {}
420
421 // GC support
422 virtual void follow_contents() {}
423 virtual void oop_iterate(OopClosure* blk) {}
424 virtual void oop_iterate_m(OopClosure* blk, MemRegion mr) {}
425 virtual void adjust_pointers() {}
426
427 #ifndef SERIALGC
428 // Parallel old support
429 virtual void follow_contents(ParCompactionManager* cm) {}
430 virtual void update_pointers() {}
431 virtual void update_pointers(HeapWord* beg_addr, HeapWord* end_addr) {}
432 #endif // SERIALGC
433
434 // CI translation: ProfileData can represent both MethodDataOop data
435 // as well as CIMethodData data. This function is provided for translating
436 // an oop in a ProfileData to the ci equivalent. Generally speaking,
437 // most ProfileData don't require any translation, so we provide the null
438 // translation here, and the required translators are in the ci subclasses.
439 virtual void translate_from(ProfileData* data) {}
440
441 virtual void print_data_on(outputStream* st) {
442 ShouldNotReachHere();
443 }
444
445 #ifndef PRODUCT
446 void print_shared(outputStream* st, const char* name);
447 void tab(outputStream* st);
448 #endif
449 };
450
451 // BitData
452 //
453 // A BitData holds a flag or two in its header.
454 class BitData : public ProfileData {
455 protected:
456 enum {
457 // null_seen:
458 // saw a null operand (cast/aastore/instanceof)
459 null_seen_flag = DataLayout::first_flag + 0
460 };
461 enum { bit_cell_count = 0 }; // no additional data fields needed.
462 public:
463 BitData(DataLayout* layout) : ProfileData(layout) {
464 }
465
466 virtual bool is_BitData() { return true; }
467
468 static int static_cell_count() {
469 return bit_cell_count;
470 }
471
472 virtual int cell_count() {
473 return static_cell_count();
474 }
475
476 // Accessor
477
478 // The null_seen flag bit is specially known to the interpreter.
479 // Consulting it allows the compiler to avoid setting up null_check traps.
480 bool null_seen() { return flag_at(null_seen_flag); }
481 void set_null_seen() { set_flag_at(null_seen_flag); }
482
483
484 // Code generation support
485 static int null_seen_byte_constant() {
486 return flag_number_to_byte_constant(null_seen_flag);
487 }
488
489 static ByteSize bit_data_size() {
490 return cell_offset(bit_cell_count);
491 }
492
493 #ifndef PRODUCT
494 void print_data_on(outputStream* st);
495 #endif
496 };
497
498 // CounterData
499 //
500 // A CounterData corresponds to a simple counter.
501 class CounterData : public BitData {
502 protected:
503 enum {
504 count_off,
505 counter_cell_count
506 };
507 public:
508 CounterData(DataLayout* layout) : BitData(layout) {}
509
510 virtual bool is_CounterData() { return true; }
511
512 static int static_cell_count() {
513 return counter_cell_count;
514 }
515
516 virtual int cell_count() {
517 return static_cell_count();
518 }
519
520 // Direct accessor
521 uint count() {
522 return uint_at(count_off);
523 }
524
525 // Code generation support
526 static ByteSize count_offset() {
527 return cell_offset(count_off);
528 }
529 static ByteSize counter_data_size() {
530 return cell_offset(counter_cell_count);
531 }
532
533 #ifndef PRODUCT
534 void print_data_on(outputStream* st);
535 #endif
536 };
537
538 // JumpData
539 //
540 // A JumpData is used to access profiling information for a direct
541 // branch. It is a counter, used for counting the number of branches,
542 // plus a data displacement, used for realigning the data pointer to
543 // the corresponding target bci.
544 class JumpData : public ProfileData {
545 protected:
546 enum {
547 taken_off_set,
548 displacement_off_set,
549 jump_cell_count
550 };
551
552 void set_displacement(int displacement) {
553 set_int_at(displacement_off_set, displacement);
554 }
555
556 public:
557 JumpData(DataLayout* layout) : ProfileData(layout) {
558 assert(layout->tag() == DataLayout::jump_data_tag ||
559 layout->tag() == DataLayout::branch_data_tag, "wrong type");
560 }
561
562 virtual bool is_JumpData() { return true; }
563
564 static int static_cell_count() {
565 return jump_cell_count;
566 }
567
568 virtual int cell_count() {
569 return static_cell_count();
570 }
571
572 // Direct accessor
573 uint taken() {
574 return uint_at(taken_off_set);
575 }
576 // Saturating counter
577 uint inc_taken() {
578 uint cnt = taken() + 1;
579 // Did we wrap? Will compiler screw us??
580 if (cnt == 0) cnt--;
581 set_uint_at(taken_off_set, cnt);
582 return cnt;
583 }
584
585 int displacement() {
586 return int_at(displacement_off_set);
587 }
588
589 // Code generation support
590 static ByteSize taken_offset() {
591 return cell_offset(taken_off_set);
592 }
593
594 static ByteSize displacement_offset() {
595 return cell_offset(displacement_off_set);
596 }
597
598 // Specific initialization.
599 void post_initialize(BytecodeStream* stream, methodDataOop mdo);
600
601 #ifndef PRODUCT
602 void print_data_on(outputStream* st);
603 #endif
604 };
605
606 // ReceiverTypeData
607 //
608 // A ReceiverTypeData is used to access profiling information about a
609 // dynamic type check. It consists of a counter which counts the total times
610 // that the check is reached, and a series of (klassOop, count) pairs
611 // which are used to store a type profile for the receiver of the check.
612 class ReceiverTypeData : public CounterData {
613 protected:
614 enum {
615 receiver0_offset = counter_cell_count,
616 count0_offset,
617 receiver_type_row_cell_count = (count0_offset + 1) - receiver0_offset
618 };
619
620 public:
621 ReceiverTypeData(DataLayout* layout) : CounterData(layout) {
622 assert(layout->tag() == DataLayout::receiver_type_data_tag ||
623 layout->tag() == DataLayout::virtual_call_data_tag, "wrong type");
624 }
625
626 virtual bool is_ReceiverTypeData() { return true; }
627
628 static int static_cell_count() {
629 return counter_cell_count + (uint) TypeProfileWidth * receiver_type_row_cell_count;
630 }
631
632 virtual int cell_count() {
633 return static_cell_count();
634 }
635
636 // Direct accessors
637 static uint row_limit() {
638 return TypeProfileWidth;
639 }
640 static int receiver_cell_index(uint row) {
641 return receiver0_offset + row * receiver_type_row_cell_count;
642 }
643 static int receiver_count_cell_index(uint row) {
644 return count0_offset + row * receiver_type_row_cell_count;
645 }
646
647 // Get the receiver at row. The 'unchecked' version is needed by parallel old
648 // gc; it does not assert the receiver is a klass. During compaction of the
649 // perm gen, the klass may already have moved, so the is_klass() predicate
650 // would fail. The 'normal' version should be used whenever possible.
651 klassOop receiver_unchecked(uint row) {
652 assert(row < row_limit(), "oob");
653 oop recv = oop_at(receiver_cell_index(row));
654 return (klassOop)recv;
655 }
656
657 klassOop receiver(uint row) {
658 klassOop recv = receiver_unchecked(row);
659 assert(recv == NULL || ((oop)recv)->is_klass(), "wrong type");
660 return recv;
661 }
662
663 uint receiver_count(uint row) {
664 assert(row < row_limit(), "oob");
665 return uint_at(receiver_count_cell_index(row));
666 }
667
668 // Code generation support
669 static ByteSize receiver_offset(uint row) {
670 return cell_offset(receiver_cell_index(row));
671 }
672 static ByteSize receiver_count_offset(uint row) {
673 return cell_offset(receiver_count_cell_index(row));
674 }
675 static ByteSize receiver_type_data_size() {
676 return cell_offset(static_cell_count());
677 }
678
679 // GC support
680 virtual void follow_contents();
681 virtual void oop_iterate(OopClosure* blk);
682 virtual void oop_iterate_m(OopClosure* blk, MemRegion mr);
683 virtual void adjust_pointers();
684
685 #ifndef SERIALGC
686 // Parallel old support
687 virtual void follow_contents(ParCompactionManager* cm);
688 virtual void update_pointers();
689 virtual void update_pointers(HeapWord* beg_addr, HeapWord* end_addr);
690 #endif // SERIALGC
691
692 oop* adr_receiver(uint row) {
693 return adr_oop_at(receiver_cell_index(row));
694 }
695
696 #ifndef PRODUCT
697 void print_receiver_data_on(outputStream* st);
698 void print_data_on(outputStream* st);
699 #endif
700 };
701
702 // VirtualCallData
703 //
704 // A VirtualCallData is used to access profiling information about a
705 // virtual call. For now, it has nothing more than a ReceiverTypeData.
706 class VirtualCallData : public ReceiverTypeData {
707 public:
708 VirtualCallData(DataLayout* layout) : ReceiverTypeData(layout) {
709 assert(layout->tag() == DataLayout::virtual_call_data_tag, "wrong type");
710 }
711
712 virtual bool is_VirtualCallData() { return true; }
713
714 static int static_cell_count() {
715 // At this point we could add more profile state, e.g., for arguments.
716 // But for now it's the same size as the base record type.
717 return ReceiverTypeData::static_cell_count();
718 }
719
720 virtual int cell_count() {
721 return static_cell_count();
722 }
723
724 // Direct accessors
725 static ByteSize virtual_call_data_size() {
726 return cell_offset(static_cell_count());
727 }
728
729 #ifndef PRODUCT
730 void print_data_on(outputStream* st);
731 #endif
732 };
733
734 // RetData
735 //
736 // A RetData is used to access profiling information for a ret bytecode.
737 // It is composed of a count of the number of times that the ret has
738 // been executed, followed by a series of triples of the form
739 // (bci, count, di) which count the number of times that some bci was the
740 // target of the ret and cache a corresponding data displacement.
741 class RetData : public CounterData {
742 protected:
743 enum {
744 bci0_offset = counter_cell_count,
745 count0_offset,
746 displacement0_offset,
747 ret_row_cell_count = (displacement0_offset + 1) - bci0_offset
748 };
749
750 void set_bci(uint row, int bci) {
751 assert((uint)row < row_limit(), "oob");
752 set_int_at(bci0_offset + row * ret_row_cell_count, bci);
753 }
754 void release_set_bci(uint row, int bci) {
755 assert((uint)row < row_limit(), "oob");
756 // 'release' when setting the bci acts as a valid flag for other
757 // threads wrt bci_count and bci_displacement.
758 release_set_int_at(bci0_offset + row * ret_row_cell_count, bci);
759 }
760 void set_bci_count(uint row, uint count) {
761 assert((uint)row < row_limit(), "oob");
762 set_uint_at(count0_offset + row * ret_row_cell_count, count);
763 }
764 void set_bci_displacement(uint row, int disp) {
765 set_int_at(displacement0_offset + row * ret_row_cell_count, disp);
766 }
767
768 public:
769 RetData(DataLayout* layout) : CounterData(layout) {
770 assert(layout->tag() == DataLayout::ret_data_tag, "wrong type");
771 }
772
773 virtual bool is_RetData() { return true; }
774
775 enum {
776 no_bci = -1 // value of bci when bci1/2 are not in use.
777 };
778
779 static int static_cell_count() {
780 return counter_cell_count + (uint) BciProfileWidth * ret_row_cell_count;
781 }
782
783 virtual int cell_count() {
784 return static_cell_count();
785 }
786
787 static uint row_limit() {
788 return BciProfileWidth;
789 }
790 static int bci_cell_index(uint row) {
791 return bci0_offset + row * ret_row_cell_count;
792 }
793 static int bci_count_cell_index(uint row) {
794 return count0_offset + row * ret_row_cell_count;
795 }
796 static int bci_displacement_cell_index(uint row) {
797 return displacement0_offset + row * ret_row_cell_count;
798 }
799
800 // Direct accessors
801 int bci(uint row) {
802 return int_at(bci_cell_index(row));
803 }
804 uint bci_count(uint row) {
805 return uint_at(bci_count_cell_index(row));
806 }
807 int bci_displacement(uint row) {
808 return int_at(bci_displacement_cell_index(row));
809 }
810
811 // Interpreter Runtime support
812 address fixup_ret(int return_bci, methodDataHandle mdo);
813
814 // Code generation support
815 static ByteSize bci_offset(uint row) {
816 return cell_offset(bci_cell_index(row));
817 }
818 static ByteSize bci_count_offset(uint row) {
819 return cell_offset(bci_count_cell_index(row));
820 }
821 static ByteSize bci_displacement_offset(uint row) {
822 return cell_offset(bci_displacement_cell_index(row));
823 }
824
825 // Specific initialization.
826 void post_initialize(BytecodeStream* stream, methodDataOop mdo);
827
828 #ifndef PRODUCT
829 void print_data_on(outputStream* st);
830 #endif
831 };
832
833 // BranchData
834 //
835 // A BranchData is used to access profiling data for a two-way branch.
836 // It consists of taken and not_taken counts as well as a data displacement
837 // for the taken case.
838 class BranchData : public JumpData {
839 protected:
840 enum {
841 not_taken_off_set = jump_cell_count,
842 branch_cell_count
843 };
844
845 void set_displacement(int displacement) {
846 set_int_at(displacement_off_set, displacement);
847 }
848
849 public:
850 BranchData(DataLayout* layout) : JumpData(layout) {
851 assert(layout->tag() == DataLayout::branch_data_tag, "wrong type");
852 }
853
854 virtual bool is_BranchData() { return true; }
855
856 static int static_cell_count() {
857 return branch_cell_count;
858 }
859
860 virtual int cell_count() {
861 return static_cell_count();
862 }
863
864 // Direct accessor
865 uint not_taken() {
866 return uint_at(not_taken_off_set);
867 }
868
869 uint inc_not_taken() {
870 uint cnt = not_taken() + 1;
871 // Did we wrap? Will compiler screw us??
872 if (cnt == 0) cnt--;
873 set_uint_at(not_taken_off_set, cnt);
874 return cnt;
875 }
876
877 // Code generation support
878 static ByteSize not_taken_offset() {
879 return cell_offset(not_taken_off_set);
880 }
881 static ByteSize branch_data_size() {
882 return cell_offset(branch_cell_count);
883 }
884
885 // Specific initialization.
886 void post_initialize(BytecodeStream* stream, methodDataOop mdo);
887
888 #ifndef PRODUCT
889 void print_data_on(outputStream* st);
890 #endif
891 };
892
893 // ArrayData
894 //
895 // A ArrayData is a base class for accessing profiling data which does
896 // not have a statically known size. It consists of an array length
897 // and an array start.
898 class ArrayData : public ProfileData {
899 protected:
900 friend class DataLayout;
901
902 enum {
903 array_len_off_set,
904 array_start_off_set
905 };
906
907 uint array_uint_at(int index) {
908 int aindex = index + array_start_off_set;
909 return uint_at(aindex);
910 }
911 int array_int_at(int index) {
912 int aindex = index + array_start_off_set;
913 return int_at(aindex);
914 }
915 oop array_oop_at(int index) {
916 int aindex = index + array_start_off_set;
917 return oop_at(aindex);
918 }
919 void array_set_int_at(int index, int value) {
920 int aindex = index + array_start_off_set;
921 set_int_at(aindex, value);
922 }
923
924 // Code generation support for subclasses.
925 static ByteSize array_element_offset(int index) {
926 return cell_offset(array_start_off_set + index);
927 }
928
929 public:
930 ArrayData(DataLayout* layout) : ProfileData(layout) {}
931
932 virtual bool is_ArrayData() { return true; }
933
934 static int static_cell_count() {
935 return -1;
936 }
937
938 int array_len() {
939 return int_at_unchecked(array_len_off_set);
940 }
941
942 virtual int cell_count() {
943 return array_len() + 1;
944 }
945
946 // Code generation support
947 static ByteSize array_len_offset() {
948 return cell_offset(array_len_off_set);
949 }
950 static ByteSize array_start_offset() {
951 return cell_offset(array_start_off_set);
952 }
953 };
954
955 // MultiBranchData
956 //
957 // A MultiBranchData is used to access profiling information for
958 // a multi-way branch (*switch bytecodes). It consists of a series
959 // of (count, displacement) pairs, which count the number of times each
960 // case was taken and specify the data displacment for each branch target.
961 class MultiBranchData : public ArrayData {
962 protected:
963 enum {
964 default_count_off_set,
965 default_disaplacement_off_set,
966 case_array_start
967 };
968 enum {
969 relative_count_off_set,
970 relative_displacement_off_set,
971 per_case_cell_count
972 };
973
974 void set_default_displacement(int displacement) {
975 array_set_int_at(default_disaplacement_off_set, displacement);
976 }
977 void set_displacement_at(int index, int displacement) {
978 array_set_int_at(case_array_start +
979 index * per_case_cell_count +
980 relative_displacement_off_set,
981 displacement);
982 }
983
984 public:
985 MultiBranchData(DataLayout* layout) : ArrayData(layout) {
986 assert(layout->tag() == DataLayout::multi_branch_data_tag, "wrong type");
987 }
988
989 virtual bool is_MultiBranchData() { return true; }
990
991 static int compute_cell_count(BytecodeStream* stream);
992
993 int number_of_cases() {
994 int alen = array_len() - 2; // get rid of default case here.
995 assert(alen % per_case_cell_count == 0, "must be even");
996 return (alen / per_case_cell_count);
997 }
998
999 uint default_count() {
1000 return array_uint_at(default_count_off_set);
1001 }
1002 int default_displacement() {
1003 return array_int_at(default_disaplacement_off_set);
1004 }
1005
1006 uint count_at(int index) {
1007 return array_uint_at(case_array_start +
1008 index * per_case_cell_count +
1009 relative_count_off_set);
1010 }
1011 int displacement_at(int index) {
1012 return array_int_at(case_array_start +
1013 index * per_case_cell_count +
1014 relative_displacement_off_set);
1015 }
1016
1017 // Code generation support
1018 static ByteSize default_count_offset() {
1019 return array_element_offset(default_count_off_set);
1020 }
1021 static ByteSize default_displacement_offset() {
1022 return array_element_offset(default_disaplacement_off_set);
1023 }
1024 static ByteSize case_count_offset(int index) {
1025 return case_array_offset() +
1026 (per_case_size() * index) +
1027 relative_count_offset();
1028 }
1029 static ByteSize case_array_offset() {
1030 return array_element_offset(case_array_start);
1031 }
1032 static ByteSize per_case_size() {
1033 return in_ByteSize(per_case_cell_count) * cell_size;
1034 }
1035 static ByteSize relative_count_offset() {
1036 return in_ByteSize(relative_count_off_set) * cell_size;
1037 }
1038 static ByteSize relative_displacement_offset() {
1039 return in_ByteSize(relative_displacement_off_set) * cell_size;
1040 }
1041
1042 // Specific initialization.
1043 void post_initialize(BytecodeStream* stream, methodDataOop mdo);
1044
1045 #ifndef PRODUCT
1046 void print_data_on(outputStream* st);
1047 #endif
1048 };
1049
1050 // methodDataOop
1051 //
1052 // A methodDataOop holds information which has been collected about
1053 // a method. Its layout looks like this:
1054 //
1055 // -----------------------------
1056 // | header |
1057 // | klass |
1058 // -----------------------------
1059 // | method |
1060 // | size of the methodDataOop |
1061 // -----------------------------
1062 // | Data entries... |
1063 // | (variable size) |
1064 // | |
1065 // . .
1066 // . .
1067 // . .
1068 // | |
1069 // -----------------------------
1070 //
1071 // The data entry area is a heterogeneous array of DataLayouts. Each
1072 // DataLayout in the array corresponds to a specific bytecode in the
1073 // method. The entries in the array are sorted by the corresponding
1074 // bytecode. Access to the data is via resource-allocated ProfileData,
1075 // which point to the underlying blocks of DataLayout structures.
1076 //
1077 // During interpretation, if profiling in enabled, the interpreter
1078 // maintains a method data pointer (mdp), which points at the entry
1079 // in the array corresponding to the current bci. In the course of
1080 // intepretation, when a bytecode is encountered that has profile data
1081 // associated with it, the entry pointed to by mdp is updated, then the
1082 // mdp is adjusted to point to the next appropriate DataLayout. If mdp
1083 // is NULL to begin with, the interpreter assumes that the current method
1084 // is not (yet) being profiled.
1085 //
1086 // In methodDataOop parlance, "dp" is a "data pointer", the actual address
1087 // of a DataLayout element. A "di" is a "data index", the offset in bytes
1088 // from the base of the data entry array. A "displacement" is the byte offset
1089 // in certain ProfileData objects that indicate the amount the mdp must be
1090 // adjusted in the event of a change in control flow.
1091 //
1092
1093 class methodDataOopDesc : public oopDesc {
1094 friend class VMStructs;
1095 private:
1096 friend class ProfileData;
1097
1098 // Back pointer to the methodOop
1099 methodOop _method;
1100
1101 // Size of this oop in bytes
1102 int _size;
1103
1104 // Cached hint for bci_to_dp and bci_to_data
1105 int _hint_di;
1106
1107 // Whole-method sticky bits and flags
1108 public:
1109 enum {
1110 _trap_hist_limit = 16, // decoupled from Deoptimization::Reason_LIMIT
1111 _trap_hist_mask = max_jubyte,
1112 _extra_data_count = 4 // extra DataLayout headers, for trap history
1113 }; // Public flag values
1114 private:
1115 uint _nof_decompiles; // count of all nmethod removals
1116 uint _nof_overflow_recompiles; // recompile count, excluding recomp. bits
1117 uint _nof_overflow_traps; // trap count, excluding _trap_hist
1118 union {
1119 intptr_t _align;
1120 u1 _array[_trap_hist_limit];
1121 } _trap_hist;
1122
1123 // Support for interprocedural escape analysis, from Thomas Kotzmann.
1124 intx _eflags; // flags on escape information
1125 intx _arg_local; // bit set of non-escaping arguments
1126 intx _arg_stack; // bit set of stack-allocatable arguments
1127 intx _arg_returned; // bit set of returned arguments
1128
1129 int _creation_mileage; // method mileage at MDO creation
1130
1131 // Size of _data array in bytes. (Excludes header and extra_data fields.)
1132 int _data_size;
1133
1134 // Beginning of the data entries
1135 intptr_t _data[1];
1136
1137 // Helper for size computation
1138 static int compute_data_size(BytecodeStream* stream);
1139 static int bytecode_cell_count(Bytecodes::Code code);
1140 enum { no_profile_data = -1, variable_cell_count = -2 };
1141
1142 // Helper for initialization
1143 DataLayout* data_layout_at(int data_index) {
1144 assert(data_index % sizeof(intptr_t) == 0, "unaligned");
1145 return (DataLayout*) (((address)_data) + data_index);
1146 }
1147
1148 // Initialize an individual data segment. Returns the size of
1149 // the segment in bytes.
1150 int initialize_data(BytecodeStream* stream, int data_index);
1151
1152 // Helper for data_at
1153 DataLayout* limit_data_position() {
1154 return (DataLayout*)((address)data_base() + _data_size);
1155 }
1156 bool out_of_bounds(int data_index) {
1157 return data_index >= data_size();
1158 }
1159
1160 // Give each of the data entries a chance to perform specific
1161 // data initialization.
1162 void post_initialize(BytecodeStream* stream);
1163
1164 // hint accessors
1165 int hint_di() const { return _hint_di; }
1166 void set_hint_di(int di) {
1167 assert(!out_of_bounds(di), "hint_di out of bounds");
1168 _hint_di = di;
1169 }
1170 ProfileData* data_before(int bci) {
1171 // avoid SEGV on this edge case
1172 if (data_size() == 0)
1173 return NULL;
1174 int hint = hint_di();
1175 if (data_layout_at(hint)->bci() <= bci)
1176 return data_at(hint);
1177 return first_data();
1178 }
1179
1180 // What is the index of the first data entry?
1181 int first_di() { return 0; }
1182
1183 // Find or create an extra ProfileData:
1184 ProfileData* bci_to_extra_data(int bci, bool create_if_missing);
1185
1186 public:
1187 static int header_size() {
1188 return sizeof(methodDataOopDesc)/wordSize;
1189 }
1190
1191 // Compute the size of a methodDataOop before it is created.
1192 static int compute_allocation_size_in_bytes(methodHandle method);
1193 static int compute_allocation_size_in_words(methodHandle method);
1194 static int compute_extra_data_count(int data_size, int empty_bc_count);
1195
1196 // Determine if a given bytecode can have profile information.
1197 static bool bytecode_has_profile(Bytecodes::Code code) {
1198 return bytecode_cell_count(code) != no_profile_data;
1199 }
1200
1201 // Perform initialization of a new methodDataOop
1202 void initialize(methodHandle method);
1203
1204 // My size
1205 int object_size_in_bytes() { return _size; }
1206 int object_size() {
1207 return align_object_size(align_size_up(_size, BytesPerWord)/BytesPerWord);
1208 }
1209
1210 int creation_mileage() const { return _creation_mileage; }
1211 void set_creation_mileage(int x) { _creation_mileage = x; }
1212 bool is_mature() const; // consult mileage and ProfileMaturityPercentage
1213 static int mileage_of(methodOop m);
1214
1215 // Support for interprocedural escape analysis, from Thomas Kotzmann.
1216 enum EscapeFlag {
1217 estimated = 1 << 0,
1218 return_local = 1 << 1
1219 };
1220
1221 intx eflags() { return _eflags; }
1222 intx arg_local() { return _arg_local; }
1223 intx arg_stack() { return _arg_stack; }
1224 intx arg_returned() { return _arg_returned; }
1225
1226 void set_eflags(intx v) { _eflags = v; }
1227 void set_arg_local(intx v) { _arg_local = v; }
1228 void set_arg_stack(intx v) { _arg_stack = v; }
1229 void set_arg_returned(intx v) { _arg_returned = v; }
1230
1231 void clear_escape_info() { _eflags = _arg_local = _arg_stack = _arg_returned = 0; }
1232
1233 // Location and size of data area
1234 address data_base() const {
1235 return (address) _data;
1236 }
1237 int data_size() {
1238 return _data_size;
1239 }
1240
1241 // Accessors
1242 methodOop method() { return _method; }
1243
1244 // Get the data at an arbitrary (sort of) data index.
1245 ProfileData* data_at(int data_index);
1246
1247 // Walk through the data in order.
1248 ProfileData* first_data() { return data_at(first_di()); }
1249 ProfileData* next_data(ProfileData* current);
1250 bool is_valid(ProfileData* current) { return current != NULL; }
1251
1252 // Convert a dp (data pointer) to a di (data index).
1253 int dp_to_di(address dp) {
1254 return dp - ((address)_data);
1255 }
1256
1257 address di_to_dp(int di) {
1258 return (address)data_layout_at(di);
1259 }
1260
1261 // bci to di/dp conversion.
1262 address bci_to_dp(int bci);
1263 int bci_to_di(int bci) {
1264 return dp_to_di(bci_to_dp(bci));
1265 }
1266
1267 // Get the data at an arbitrary bci, or NULL if there is none.
1268 ProfileData* bci_to_data(int bci);
1269
1270 // Same, but try to create an extra_data record if one is needed:
1271 ProfileData* allocate_bci_to_data(int bci) {
1272 ProfileData* data = bci_to_data(bci);
1273 return (data != NULL) ? data : bci_to_extra_data(bci, true);
1274 }
1275
1276 // Add a handful of extra data records, for trap tracking.
1277 DataLayout* extra_data_base() { return limit_data_position(); }
1278 DataLayout* extra_data_limit() { return (DataLayout*)((address)this + object_size_in_bytes()); }
1279 int extra_data_size() { return (address)extra_data_limit()
1280 - (address)extra_data_base(); }
1281 static DataLayout* next_extra(DataLayout* dp) { return (DataLayout*)((address)dp + in_bytes(DataLayout::cell_offset(0))); }
1282
1283 // Return (uint)-1 for overflow.
1284 uint trap_count(int reason) const {
1285 assert((uint)reason < _trap_hist_limit, "oob");
1286 return (int)((_trap_hist._array[reason]+1) & _trap_hist_mask) - 1;
1287 }
1288 // For loops:
1289 static uint trap_reason_limit() { return _trap_hist_limit; }
1290 static uint trap_count_limit() { return _trap_hist_mask; }
1291 uint inc_trap_count(int reason) {
1292 // Count another trap, anywhere in this method.
1293 assert(reason >= 0, "must be single trap");
1294 if ((uint)reason < _trap_hist_limit) {
1295 uint cnt1 = 1 + _trap_hist._array[reason];
1296 if ((cnt1 & _trap_hist_mask) != 0) { // if no counter overflow...
1297 _trap_hist._array[reason] = cnt1;
1298 return cnt1;
1299 } else {
1300 return _trap_hist_mask + (++_nof_overflow_traps);
1301 }
1302 } else {
1303 // Could not represent the count in the histogram.
1304 return (++_nof_overflow_traps);
1305 }
1306 }
1307
1308 uint overflow_trap_count() const {
1309 return _nof_overflow_traps;
1310 }
1311 uint overflow_recompile_count() const {
1312 return _nof_overflow_recompiles;
1313 }
1314 void inc_overflow_recompile_count() {
1315 _nof_overflow_recompiles += 1;
1316 }
1317 uint decompile_count() const {
1318 return _nof_decompiles;
1319 }
1320 void inc_decompile_count() {
1321 _nof_decompiles += 1;
1322 }
1323
1324 // Support for code generation
1325 static ByteSize data_offset() {
1326 return byte_offset_of(methodDataOopDesc, _data[0]);
1327 }
1328
1329 // GC support
1330 oop* adr_method() const { return (oop*)&_method; }
1331 bool object_is_parsable() const { return _size != 0; }
1332 void set_object_is_parsable(int object_size_in_bytes) { _size = object_size_in_bytes; }
1333
1334 #ifndef PRODUCT
1335 // printing support for method data
1336 void print_data_on(outputStream* st);
1337 #endif
1338
1339 // verification
1340 void verify_data_on(outputStream* st);
1341 };