comparison graal/com.oracle.max.graal.hotspot/src/com/oracle/max/graal/hotspot/ri/HotSpotMethodData.java @ 4439:f7251c729b31

profiling info first try
author Christian Haeubl <christian.haeubl@oracle.com>
date Thu, 19 Jan 2012 16:29:35 -0800
parents
children 271220b49abc
comparison
equal deleted inserted replaced
4288:2bc254976621 4439:f7251c729b31
1 /*
2 * Copyright (c) 2011, 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 package com.oracle.max.graal.hotspot.ri;
24
25 import sun.misc.*;
26
27 import com.oracle.max.cri.ri.*;
28 import com.oracle.max.graal.hotspot.*;
29 import com.oracle.max.graal.hotspot.Compiler;
30
31
32 public final class HotSpotMethodData extends CompilerObject {
33
34 /**
35 *
36 */
37 private static final long serialVersionUID = -8873133496591225071L;
38 // TODO (ch) use same logic as in NodeClass?
39 private static final Unsafe unsafe = Unsafe.getUnsafe();
40 private static final HotSpotMethodDataAccessor[] PROFILE_DATA_ACCESSORS = {
41 null, new BitData(), new CounterData(), new JumpData(),
42 new TypeCheckData(), new VirtualCallData(), new RetData(),
43 new BranchData(), new MultiBranchData(), new ArgInfoData()
44 };
45
46 private Object javaMirror;
47 private int dataSize;
48
49 private HotSpotMethodData(Compiler compiler) {
50 super(compiler);
51 javaMirror = null;
52 dataSize = 0;
53 throw new IllegalStateException("this constructor is never actually called, because the objects are allocated from within the VM");
54 }
55
56 public HotSpotMethodDataAccessor getDataAccessor(int position) {
57 if (position < 0 || position >= dataSize) {
58 return null;
59 }
60
61 int tag = AbstractMethodDataAccessor.readTag(this, position);
62 assert tag > 0 && tag < PROFILE_DATA_ACCESSORS.length : "illegal tag";
63 return PROFILE_DATA_ACCESSORS[tag];
64 }
65
66 private int readUnsignedByte(int position, int offsetInCells) {
67 long fullOffset = computeFullOffset(position, offsetInCells);
68 return unsafe.getByte(javaMirror, fullOffset) & 0xFF;
69 }
70
71 private int readUnsignedShort(int position, int offsetInCells) {
72 long fullOffset = computeFullOffset(position, offsetInCells);
73 return unsafe.getShort(javaMirror, fullOffset) & 0xFFFF;
74 }
75
76 private long readUnsignedInt(int position, int offsetInCells) {
77 long fullOffset = computeFullOffset(position, offsetInCells);
78 return unsafe.getInt(javaMirror, fullOffset) & 0xFFFFFFFFL;
79 }
80
81 private int readInt(int position, int offsetInCells) {
82 long fullOffset = computeFullOffset(position, offsetInCells);
83 return unsafe.getInt(javaMirror, fullOffset);
84 }
85
86 private static int computeFullOffset(int position, int offsetInCells) {
87 HotSpotVMConfig config = getHotSpotVMConfig();
88 return config.methodDataDataOffset + position + offsetInCells * config.dataLayoutCellSize;
89 }
90
91 private static HotSpotVMConfig getHotSpotVMConfig() {
92 // TODO: implement, cache config somewhere?
93 return null;
94 }
95
96 private abstract static class AbstractMethodDataAccessor implements HotSpotMethodDataAccessor {
97 private final int tag;
98 private final int staticCellCount;
99
100 protected AbstractMethodDataAccessor(int tag, int staticCellCount) {
101 this.tag = tag;
102 this.staticCellCount = staticCellCount;
103 }
104
105 @Override
106 public int getTag() {
107 return tag;
108 }
109
110 public static int readTag(HotSpotMethodData data, int position) {
111 HotSpotVMConfig config = getHotSpotVMConfig();
112 return data.readUnsignedByte(position, config.dataLayoutTagOffset);
113 }
114
115 @Override
116 public int getBCI(HotSpotMethodData data, int position) {
117 HotSpotVMConfig config = getHotSpotVMConfig();
118 return data.readUnsignedShort(position, config.dataLayoutBCIOffset);
119 }
120
121 @Override
122 public int getSize(HotSpotMethodData data, int position) {
123 HotSpotVMConfig config = getHotSpotVMConfig();
124 return config.dataLayoutHeaderSizeInBytes + (staticCellCount + getDynamicCellCount(data, position)) * config.dataLayoutCellSize;
125 }
126
127 @Override
128 public boolean hasExceptionOccurred(HotSpotMethodData data, int position) {
129 return false;
130 }
131
132 @Override
133 public RiResolvedType[] getTypes(HotSpotMethodData data, int position) {
134 throw new IllegalStateException("not supported by current method data");
135 }
136
137 @Override
138 public double[] getTypeProbabilities(HotSpotMethodData data, int position) {
139 throw new IllegalStateException("not supported by current method data");
140 }
141
142 @Override
143 public double getBranchTakenProbability(HotSpotMethodData data, int position) {
144 throw new IllegalStateException("not supported by current method data");
145 }
146
147 @Override
148 public double[] getSwitchProbabilities(HotSpotMethodData data, int position) {
149 throw new IllegalStateException("not supported by current method data");
150 }
151
152 @Override
153 public long getExecutionCount(HotSpotMethodData data, int position) {
154 throw new IllegalStateException("not supported by current method data");
155 }
156
157 protected int getDynamicCellCount(HotSpotMethodData data, int position) {
158 return 0;
159 }
160 }
161
162 private static class BitData extends AbstractMethodDataAccessor {
163 private static final int BIT_DATA_TAG = 1;
164 private static final int BIT_DATA_CELLS = 0;
165
166 private BitData() {
167 super(BIT_DATA_TAG, BIT_DATA_CELLS);
168 }
169
170 protected BitData(int tag, int staticCellCount) {
171 super(tag, staticCellCount);
172 }
173 }
174
175 private static class CounterData extends BitData {
176 private static final int COUNTER_DATA_TAG = 2;
177 private static final int COUNTER_DATA_CELLS = 1;
178 private static final int COUNTER_DATA_COUNT_OFFSET = 0;
179
180 public CounterData() {
181 super(COUNTER_DATA_TAG, COUNTER_DATA_CELLS);
182 }
183
184 protected CounterData(int tag, int staticCellCount) {
185 super(tag, staticCellCount);
186 }
187
188 @Override
189 public long getExecutionCount(HotSpotMethodData data, int position) {
190 return getCounterValue(data, position);
191 }
192
193 protected long getCounterValue(HotSpotMethodData data, int position) {
194 return data.readUnsignedInt(position, COUNTER_DATA_COUNT_OFFSET);
195 }
196 }
197
198 private static class JumpData extends AbstractMethodDataAccessor {
199 private static final int JUMP_DATA_TAG = 3;
200 private static final int JUMP_DATA_CELLS = 2;
201 protected static final int TAKEN_COUNT_OFFSET = 0;
202 protected static final int TAKEN_DISPLACEMENT_OFFSET = 1;
203
204 public JumpData() {
205 super(JUMP_DATA_TAG, JUMP_DATA_CELLS);
206 }
207
208 protected JumpData(int tag, int staticCellCount) {
209 super(tag, staticCellCount);
210 }
211
212 @Override
213 public double getBranchTakenProbability(HotSpotMethodData data, int position) {
214 return 1;
215 }
216
217 @Override
218 public long getExecutionCount(HotSpotMethodData data, int position) {
219 return data.readUnsignedInt(position, TAKEN_COUNT_OFFSET);
220 }
221
222 public int getDisplacement(HotSpotMethodData data, int position) {
223 return data.readInt(position, TAKEN_DISPLACEMENT_OFFSET);
224 }
225 }
226
227 private static class AbstractTypeData extends CounterData {
228 private static final int RECEIVER_TYPE_DATA_ROW_CELL_COUNT = 2;
229 private static final int RECEIVER_TYPE_DATA_FIRST_RECEIVER_OFFSET = 1;
230 private static final int RECEIVER_TYPE_DATA_FIRST_COUNT_OFFSET = 2;
231
232 protected AbstractTypeData(int tag, int staticCellCount) {
233 super(tag, staticCellCount);
234 }
235
236 @Override
237 public double[] getTypeProbabilities(HotSpotMethodData data, int position) {
238 HotSpotVMConfig config = getHotSpotVMConfig();
239 int typeProfileWidth = config.typeProfileWidth;
240
241 long total = 0;
242 double[] result = new double[typeProfileWidth];
243
244 for (int i = 0; i < typeProfileWidth; i++) {
245 long count = data.readUnsignedInt(position, getCountOffset(i));
246 total += count;
247 result[i] = count;
248 }
249
250 if (total != 0) {
251 for (int i = 0; i < typeProfileWidth; i++) {
252 result[i] = result[i] / total;
253 }
254 }
255 return result;
256 }
257
258 @Override
259 public RiResolvedType[] getTypes(HotSpotMethodData data, int position) {
260 // TODO: seems to require a native call...
261 return null;
262 }
263
264 @Override
265 protected int getDynamicCellCount(HotSpotMethodData data, int position) {
266 HotSpotVMConfig config = getHotSpotVMConfig();
267 return config.typeProfileWidth * RECEIVER_TYPE_DATA_ROW_CELL_COUNT;
268 }
269
270 private static int getReceiverOffset(int row) {
271 return RECEIVER_TYPE_DATA_FIRST_RECEIVER_OFFSET + row * RECEIVER_TYPE_DATA_ROW_CELL_COUNT;
272 }
273
274 protected static int getCountOffset(int row) {
275 return RECEIVER_TYPE_DATA_FIRST_COUNT_OFFSET + row * RECEIVER_TYPE_DATA_ROW_CELL_COUNT;
276 }
277 }
278
279 private static class TypeCheckData extends AbstractTypeData {
280 private static final int RECEIVER_TYPE_DATA_TAG = 4;
281 private static final int RECEIVER_TYPE_DATA_CELLS = 1;
282
283 public TypeCheckData() {
284 super(RECEIVER_TYPE_DATA_TAG, RECEIVER_TYPE_DATA_CELLS);
285 }
286
287 @Override
288 public long getExecutionCount(HotSpotMethodData data, int position) {
289 throw new IllegalStateException("not supported by current method data");
290 }
291
292 public long getTypeCheckFailedCount(HotSpotMethodData data, int position) {
293 return super.getCounterValue(data, position);
294 }
295 }
296
297 private static class VirtualCallData extends AbstractTypeData {
298 private static final int VIRTUAL_CALL_DATA_TAG = 5;
299 private static final int VIRTUAL_CALL_DATA_CELLS = 1;
300
301 public VirtualCallData() {
302 super(VIRTUAL_CALL_DATA_TAG, VIRTUAL_CALL_DATA_CELLS);
303 }
304
305 @Override
306 public long getExecutionCount(HotSpotMethodData data, int position) {
307 HotSpotVMConfig config = getHotSpotVMConfig();
308 int typeProfileWidth = config.typeProfileWidth;
309
310 long total = 0;
311 for (int i = 0; i < typeProfileWidth; i++) {
312 total += data.readUnsignedInt(position, getCountOffset(i));
313 }
314
315 return total + getCounterValue(data, position);
316 }
317 }
318
319 private static class RetData extends CounterData {
320 private static final int RET_DATA_TAG = 6;
321 private static final int RET_DATA_CELLS = 1;
322 private static final int RET_DATA_ROW_CELL_COUNT = 3;
323
324 public RetData() {
325 super(RET_DATA_TAG, RET_DATA_CELLS);
326 }
327
328 @Override
329 protected int getDynamicCellCount(HotSpotMethodData data, int position) {
330 HotSpotVMConfig config = getHotSpotVMConfig();
331 return config.bciProfileWidth * RET_DATA_ROW_CELL_COUNT;
332 }
333 }
334
335 private static class BranchData extends JumpData {
336 private static final int BRANCH_DATA_TAG = 7;
337 private static final int BRANCH_DATA_CELLS = 3;
338 private static final int NOT_TAKEN_COUNT_OFFSET = 2;
339
340 public BranchData() {
341 super(BRANCH_DATA_TAG, BRANCH_DATA_CELLS);
342 }
343
344 @Override
345 public double getBranchTakenProbability(HotSpotMethodData data, int position) {
346 long takenCount = data.readUnsignedInt(position, TAKEN_COUNT_OFFSET);
347 long notTakenCount = data.readUnsignedInt(position, NOT_TAKEN_COUNT_OFFSET);
348 double total = takenCount + notTakenCount;
349 return takenCount / total;
350 }
351 }
352
353 private static class ArrayData extends AbstractMethodDataAccessor {
354 private static final int ARRAY_DATA_LENGTH_OFFSET = 0;
355 private static final int ARRAY_DATA_START_OFFSET = 1;
356
357 public ArrayData(int tag, int staticCellCount) {
358 super(tag, staticCellCount);
359 }
360
361 @Override
362 protected int getDynamicCellCount(HotSpotMethodData data, int position) {
363 return getLength(data, position);
364 }
365
366 protected static int getLength(HotSpotMethodData data, int position) {
367 return data.readInt(position, ARRAY_DATA_LENGTH_OFFSET);
368 }
369
370 protected static int getElementOffset(int index) {
371 return ARRAY_DATA_START_OFFSET + index;
372 }
373 }
374
375 private static class MultiBranchData extends ArrayData {
376 private static final int MULTI_BRANCH_DATA_TAG = 8;
377 private static final int MULTI_BRANCH_DATA_CELLS = 1;
378 private static final int MULTI_BRANCH_DATA_COUNT_OFFSET = 0;
379 private static final int MULTI_BRANCH_DATA_DISPLACEMENT_OFFSET = 1;
380
381 public MultiBranchData() {
382 super(MULTI_BRANCH_DATA_TAG, MULTI_BRANCH_DATA_CELLS);
383 }
384
385 @Override
386 public double[] getSwitchProbabilities(HotSpotMethodData data, int position) {
387 int length = getLength(data, position);
388 long total = 0;
389 double[] result = new double[length];
390
391 for (int i = 0; i < length; i++) {
392 int offset = getCountOffset(i);
393 long count = data.readUnsignedInt(position, offset);
394 total += count;
395 result[i] = count;
396 }
397
398 if (total != 0) {
399 for (int i = 0; i < length; i++) {
400 result[i] = result[i] / total;
401 }
402 }
403 return result;
404 }
405
406 @Override
407 public long getExecutionCount(HotSpotMethodData data, int position) {
408 int length = getLength(data, position);
409 long total = 0;
410
411 for (int i = 0; i < length; i++) {
412 int offset = getCountOffset(i);
413 total += data.readUnsignedInt(position, offset);
414 }
415
416 return total;
417 }
418
419 private static int getCountOffset(int index) {
420 return getElementOffset(index + MULTI_BRANCH_DATA_COUNT_OFFSET);
421 }
422 }
423
424 private static class ArgInfoData extends ArrayData {
425 private static final int ARG_INFO_DATA_TAG = 9;
426 private static final int ARG_INFO_DATA_CELLS = 1;
427
428 public ArgInfoData() {
429 super(ARG_INFO_DATA_TAG, ARG_INFO_DATA_CELLS);
430 }
431 }
432 }