comparison graal/com.oracle.max.criutils/src/com/oracle/max/criutils/SnapshotProfilingInfo.java @ 5346:4c3d953f8131

added mechanism (enabled by -G:PICache and -G:PiFilter) for saving/loading method profiling info to/from disk
author Doug Simon <doug.simon@oracle.com>
date Thu, 03 May 2012 13:39:45 +0200
parents
children 56860d3f9f39
comparison
equal deleted inserted replaced
5345:00803ae428d2 5346:4c3d953f8131
1 /*
2 * Copyright (c) 2012, 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.criutils;
24
25 import java.io.*;
26
27 import com.oracle.max.cri.ci.*;
28 import com.oracle.max.cri.ri.*;
29
30 /**
31 * A profiling info snapshot that can be {@linkplain #save(File, File) saved} to
32 * and {@linkplain #load(File, RiRuntime) loaded} from a file.
33 */
34 public class SnapshotProfilingInfo implements RiProfilingInfo, Serializable {
35
36 private static final long serialVersionUID = -5837615128782960391L;
37 private final double[] branchTaken;
38 private final double[][] switches;
39 private final RiTypeProfile[] typeProfiles;
40 private final RiExceptionSeen[] exceptions;
41 private final int[] executions;
42 private final int[] deopts;
43
44 public SnapshotProfilingInfo(RiProfilingInfo other) {
45 int codeSize = other.codeSize();
46 branchTaken = new double[codeSize];
47 switches = new double[codeSize][];
48 typeProfiles = new RiTypeProfile[codeSize];
49 exceptions = new RiExceptionSeen[codeSize];
50 executions = new int[codeSize];
51 deopts = new int[RiDeoptReason.values().length];
52
53 for (int bci = 0; bci < codeSize; bci++) {
54 executions[bci] = other.getExecutionCount(bci);
55 exceptions[bci] = other.getExceptionSeen(bci);
56 branchTaken[bci] = other.getBranchTakenProbability(bci);
57 switches[bci] = other.getSwitchProbabilities(bci);
58 typeProfiles[bci] = other.getTypeProfile(bci);
59 }
60 for (RiDeoptReason reason: RiDeoptReason.values()) {
61 deopts[reason.ordinal()] = other.getDeoptimizationCount(reason);
62 }
63 }
64
65 @Override
66 public int codeSize() {
67 return branchTaken.length;
68 }
69
70 public double getBranchTakenProbability(int bci) {
71 return bci < branchTaken.length ? branchTaken[bci] : -1D;
72 }
73 public double[] getSwitchProbabilities(int bci) {
74 return bci < switches.length ? switches[bci] : null;
75 }
76 public RiTypeProfile getTypeProfile(int bci) {
77 return bci < typeProfiles.length ? typeProfiles[bci] : null;
78 }
79 public RiExceptionSeen getExceptionSeen(int bci) {
80 return bci < exceptions.length ? exceptions[bci] : RiExceptionSeen.NOT_SUPPORTED;
81 }
82 public int getExecutionCount(int bci) {
83 return bci < executions.length ? executions[bci] : -1;
84 }
85 public int getDeoptimizationCount(RiDeoptReason reason) {
86 return deopts[reason.ordinal()];
87 }
88
89 @Override
90 public String toString() {
91 return CiUtil.profileToString(this, null, "; ");
92 }
93
94 /**
95 * Deserializes a profile snapshot from a file.
96 *
97 * @param file a file created by {@link #save(File, File)}
98 * @param runtime the runtime used to resolve {@link RiResolvedType}s during deserialization
99 * @return
100 * @throws ClassNotFoundException
101 * @throws IOException
102 */
103 public static SnapshotProfilingInfo load(File file, RiRuntime runtime) throws ClassNotFoundException, IOException {
104 SnapshotProfilingInfo.SnapshotObjectInputStream ois = new SnapshotObjectInputStream(new BufferedInputStream(new FileInputStream(file), (int) file.length()), runtime);
105 try {
106 return (SnapshotProfilingInfo) ois.readObject();
107 } finally {
108 ois.close();
109 }
110 }
111
112 /**
113 * Serializes this snapshot to a file.
114 *
115 * @param file the file to which this snapshot is serialized
116 * @param txtFile
117 * @throws IOException
118 */
119 public void save(File file, File txtFile) throws IOException {
120 SnapshotProfilingInfo.SnapshotObjectOutputStream oos = new SnapshotObjectOutputStream(new FileOutputStream(file));
121 try {
122 oos.writeObject(this);
123 } finally {
124 oos.close();
125 }
126 if (txtFile != null) {
127 PrintStream out = new PrintStream(txtFile);
128 try {
129 out.println(CiUtil.profileToString(this, null, CiUtil.NEW_LINE));
130 } finally {
131 out.close();
132 }
133 }
134 }
135
136 static class RiResolvedTypePlaceholder implements Serializable {
137 private static final long serialVersionUID = -5149982457010023916L;
138 final Class javaMirror;
139 public RiResolvedTypePlaceholder(Class javaMirror) {
140 this.javaMirror = javaMirror;
141 }
142 }
143
144 static class SnapshotObjectOutputStream extends ObjectOutputStream {
145 public SnapshotObjectOutputStream(OutputStream out) throws IOException {
146 super(out);
147 enableReplaceObject(true);
148 }
149
150 @Override
151 protected Object replaceObject(Object obj) throws IOException {
152 if (obj instanceof RiResolvedType) {
153 return new RiResolvedTypePlaceholder(((RiResolvedType) obj).toJava());
154 }
155 return obj;
156 }
157 }
158
159 static class SnapshotObjectInputStream extends ObjectInputStream {
160 private final RiRuntime runtime;
161 public SnapshotObjectInputStream(InputStream in, RiRuntime runtime) throws IOException {
162 super(in);
163 enableResolveObject(true);
164 this.runtime = runtime;
165 }
166
167 @Override
168 protected Object resolveObject(Object obj) throws IOException {
169 if (obj instanceof SnapshotProfilingInfo.RiResolvedTypePlaceholder) {
170 RiResolvedType type = runtime.getType(((SnapshotProfilingInfo.RiResolvedTypePlaceholder) obj).javaMirror);
171 return type;
172 }
173 return obj;
174 }
175 }
176 }