Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.profiles/src/com/oracle/truffle/api/profiles/BranchProfile.java @ 22503:828c67903db2
Moving profiles into their own project to ensure the core API doesn't reference these utility classes.
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Thu, 17 Dec 2015 10:01:38 +0100 |
parents | truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/profiles/BranchProfile.java@a63bda98cfdb |
children | d80a5ff56f51 |
comparison
equal
deleted
inserted
replaced
22502:d2b4fe945c23 | 22503:828c67903db2 |
---|---|
1 /* | |
2 * Copyright (c) 2013, 2015, 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. Oracle designates this | |
8 * particular file as subject to the "Classpath" exception as provided | |
9 * by Oracle in the LICENSE file that accompanied this code. | |
10 * | |
11 * This code is distributed in the hope that it will be useful, but WITHOUT | |
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 * version 2 for more details (a copy is included in the LICENSE file that | |
15 * accompanied this code). | |
16 * | |
17 * You should have received a copy of the GNU General Public License version | |
18 * 2 along with this work; if not, write to the Free Software Foundation, | |
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
20 * | |
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
22 * or visit www.oracle.com if you need additional information or have any | |
23 * questions. | |
24 */ | |
25 package com.oracle.truffle.api.profiles; | |
26 | |
27 import com.oracle.truffle.api.CompilerDirectives; | |
28 import com.oracle.truffle.api.CompilerDirectives.CompilationFinal; | |
29 import com.oracle.truffle.api.Truffle; | |
30 | |
31 /** | |
32 * <p> | |
33 * BranchProfiles are profiles to speculate on branches that are unlikely to be visited. If the | |
34 * {@link #enter()} method is invoked first the optimized code is invalidated and the branch where | |
35 * {@link #enter()} is invoked is enabled for compilation. Otherwise if the {@link #enter()} method | |
36 * was never invoked the branch will not get compiled. | |
37 * </p> | |
38 * | |
39 * <p> | |
40 * <b> Usage example: </b> | |
41 * | |
42 * <pre> | |
43 * class SampleNode extends Node { | |
44 * | |
45 * final BranchProfile errorProfile = BranchProfile.create(); | |
46 * | |
47 * void execute(int value) { | |
48 * if (value == Integer.MAX_VALUE) { | |
49 * errorProfile.enter(); | |
50 * throw new Error("Invalid input value") | |
51 * } | |
52 * return value; | |
53 * } | |
54 * } | |
55 * </pre> | |
56 * | |
57 * {@inheritDoc} | |
58 * | |
59 * @see BranchProfile#enter() | |
60 */ | |
61 public abstract class BranchProfile extends Profile { | |
62 | |
63 BranchProfile() { | |
64 } | |
65 | |
66 /** | |
67 * Call when an unlikely branch is entered. | |
68 */ | |
69 public abstract void enter(); | |
70 | |
71 /** | |
72 * @deprecated it is not reliable when profiling is turned off. | |
73 */ | |
74 @Deprecated | |
75 public abstract boolean isVisited(); | |
76 | |
77 /** | |
78 * Call to create a new instance of a branch profile. | |
79 */ | |
80 public static BranchProfile create() { | |
81 if (Truffle.getRuntime().isProfilingEnabled()) { | |
82 return Enabled.create0(); | |
83 } else { | |
84 return Disabled.INSTANCE; | |
85 } | |
86 } | |
87 | |
88 static final class Enabled extends BranchProfile { | |
89 | |
90 @CompilationFinal private boolean visited; | |
91 | |
92 @Override | |
93 public void enter() { | |
94 if (!visited) { | |
95 CompilerDirectives.transferToInterpreterAndInvalidate(); | |
96 visited = true; | |
97 } | |
98 } | |
99 | |
100 @SuppressWarnings("deprecation") | |
101 @Override | |
102 public boolean isVisited() { | |
103 return visited; | |
104 } | |
105 | |
106 @Override | |
107 public String toString() { | |
108 return toString(BranchProfile.class, !visited, false, "VISITED"); | |
109 } | |
110 | |
111 /* Needed for lazy class loading. */ | |
112 static BranchProfile create0() { | |
113 return new Enabled(); | |
114 } | |
115 } | |
116 | |
117 static final class Disabled extends BranchProfile { | |
118 | |
119 static final BranchProfile INSTANCE = new Disabled(); | |
120 | |
121 @Override | |
122 protected Object clone() { | |
123 return INSTANCE; | |
124 } | |
125 | |
126 @Override | |
127 public void enter() { | |
128 } | |
129 | |
130 @SuppressWarnings("deprecation") | |
131 @Override | |
132 public boolean isVisited() { | |
133 return true; | |
134 } | |
135 | |
136 @Override | |
137 public String toString() { | |
138 return toStringDisabled(BranchProfile.class); | |
139 } | |
140 | |
141 } | |
142 | |
143 } |