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 org.graalvm.compiler.core.test; 24 25 import java.io.Serializable; 26 27 import jdk.vm.ci.meta.JavaTypeProfile; 28 import jdk.vm.ci.meta.ProfilingInfo; 29 import jdk.vm.ci.meta.ResolvedJavaMethod; 30 import jdk.vm.ci.meta.ResolvedJavaType; 31 import jdk.vm.ci.meta.TriState; 32 33 import org.junit.Assert; 34 import org.junit.Test; 35 36 /** 37 * Tests profiling information provided by the runtime. 38 * <p> 39 * NOTE: These tests are actually not very robust. The problem is that only partial profiling 40 * information may be gathered for any given method. For example, HotSpot's advanced compilation 41 * policy can decide to only gather partial profiles in a first level compilation (see 42 * AdvancedThresholdPolicy::common(...) in advancedThresholdPolicy.cpp). Because of this, 43 * occasionally tests for {@link ProfilingInfo#getNullSeen(int)} can fail since HotSpot only set's 44 * the null_seen bit when doing full profiling. 45 */ 46 public class ProfilingInfoTest extends GraalCompilerTest { 47 48 private static final int N = 10; 49 private static final double DELTA = 1d / Integer.MAX_VALUE; 50 51 @Test 52 public void testBranchTakenProbability() { 53 ProfilingInfo info = profile("branchProbabilitySnippet", 0); 54 Assert.assertEquals(0.0, info.getBranchTakenProbability(1), DELTA); 55 Assert.assertEquals(N, info.getExecutionCount(1)); 56 Assert.assertEquals(-1.0, info.getBranchTakenProbability(8), DELTA); 57 Assert.assertEquals(0, info.getExecutionCount(8)); 58 59 info = profile("branchProbabilitySnippet", 1); 60 Assert.assertEquals(1.0, info.getBranchTakenProbability(1), DELTA); 61 Assert.assertEquals(N, info.getExecutionCount(1)); 62 Assert.assertEquals(0.0, info.getBranchTakenProbability(8), DELTA); 63 Assert.assertEquals(N, info.getExecutionCount(8)); 163 164 ProfilingInfo info = profile(testSnippet, "ABC"); 165 JavaTypeProfile typeProfile = info.getTypeProfile(bci); 166 Assert.assertEquals(0.0, typeProfile.getNotRecordedProbability(), DELTA); 167 Assert.assertEquals(1, typeProfile.getTypes().length); 168 Assert.assertEquals(stringType, typeProfile.getTypes()[0].getType()); 169 Assert.assertEquals(1.0, typeProfile.getTypes()[0].getProbability(), DELTA); 170 171 continueProfiling(testSnippet, new StringBuilder()); 172 typeProfile = info.getTypeProfile(bci); 173 Assert.assertEquals(0.0, typeProfile.getNotRecordedProbability(), DELTA); 174 Assert.assertEquals(2, typeProfile.getTypes().length); 175 Assert.assertEquals(stringType, typeProfile.getTypes()[0].getType()); 176 Assert.assertEquals(stringBuilderType, typeProfile.getTypes()[1].getType()); 177 Assert.assertEquals(0.5, typeProfile.getTypes()[0].getProbability(), DELTA); 178 Assert.assertEquals(0.5, typeProfile.getTypes()[1].getProbability(), DELTA); 179 180 resetProfile(testSnippet); 181 typeProfile = info.getTypeProfile(bci); 182 Assert.assertNull(typeProfile); 183 } 184 185 @Test 186 public void testExceptionSeen() { 187 // NullPointerException 188 ProfilingInfo info = profile("nullPointerExceptionSnippet", 5); 189 Assert.assertEquals(TriState.FALSE, info.getExceptionSeen(1)); 190 191 info = profile("nullPointerExceptionSnippet", (Object) null); 192 Assert.assertEquals(TriState.TRUE, info.getExceptionSeen(1)); 193 194 resetProfile("nullPointerExceptionSnippet"); 195 Assert.assertEquals(TriState.FALSE, info.getExceptionSeen(1)); 196 197 // ArrayOutOfBoundsException 198 info = profile("arrayIndexOutOfBoundsExceptionSnippet", new int[1]); 199 Assert.assertEquals(TriState.FALSE, info.getExceptionSeen(2)); 200 201 info = profile("arrayIndexOutOfBoundsExceptionSnippet", new int[0]); 202 Assert.assertEquals(TriState.TRUE, info.getExceptionSeen(2)); | 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 org.graalvm.compiler.core.test; 24 25 import java.io.Serializable; 26 27 import org.graalvm.compiler.test.SubprocessUtil; 28 import org.junit.Assert; 29 import org.junit.Assume; 30 import org.junit.Test; 31 32 import jdk.vm.ci.meta.JavaTypeProfile; 33 import jdk.vm.ci.meta.ProfilingInfo; 34 import jdk.vm.ci.meta.ResolvedJavaMethod; 35 import jdk.vm.ci.meta.ResolvedJavaType; 36 import jdk.vm.ci.meta.TriState; 37 38 /** 39 * Tests profiling information provided by the runtime. 40 * <p> 41 * NOTE: These tests are actually not very robust. The problem is that only partial profiling 42 * information may be gathered for any given method. For example, HotSpot's advanced compilation 43 * policy can decide to only gather partial profiles in a first level compilation (see 44 * AdvancedThresholdPolicy::common(...) in advancedThresholdPolicy.cpp). Because of this, 45 * occasionally tests for {@link ProfilingInfo#getNullSeen(int)} can fail since HotSpot only sets 46 * the null_seen bit when doing full profiling. 47 */ 48 public class ProfilingInfoTest extends GraalCompilerTest { 49 50 private static final int N = 10; 51 private static final double DELTA = 1d / Integer.MAX_VALUE; 52 53 @Test 54 public void testBranchTakenProbability() { 55 ProfilingInfo info = profile("branchProbabilitySnippet", 0); 56 Assert.assertEquals(0.0, info.getBranchTakenProbability(1), DELTA); 57 Assert.assertEquals(N, info.getExecutionCount(1)); 58 Assert.assertEquals(-1.0, info.getBranchTakenProbability(8), DELTA); 59 Assert.assertEquals(0, info.getExecutionCount(8)); 60 61 info = profile("branchProbabilitySnippet", 1); 62 Assert.assertEquals(1.0, info.getBranchTakenProbability(1), DELTA); 63 Assert.assertEquals(N, info.getExecutionCount(1)); 64 Assert.assertEquals(0.0, info.getBranchTakenProbability(8), DELTA); 65 Assert.assertEquals(N, info.getExecutionCount(8)); 165 166 ProfilingInfo info = profile(testSnippet, "ABC"); 167 JavaTypeProfile typeProfile = info.getTypeProfile(bci); 168 Assert.assertEquals(0.0, typeProfile.getNotRecordedProbability(), DELTA); 169 Assert.assertEquals(1, typeProfile.getTypes().length); 170 Assert.assertEquals(stringType, typeProfile.getTypes()[0].getType()); 171 Assert.assertEquals(1.0, typeProfile.getTypes()[0].getProbability(), DELTA); 172 173 continueProfiling(testSnippet, new StringBuilder()); 174 typeProfile = info.getTypeProfile(bci); 175 Assert.assertEquals(0.0, typeProfile.getNotRecordedProbability(), DELTA); 176 Assert.assertEquals(2, typeProfile.getTypes().length); 177 Assert.assertEquals(stringType, typeProfile.getTypes()[0].getType()); 178 Assert.assertEquals(stringBuilderType, typeProfile.getTypes()[1].getType()); 179 Assert.assertEquals(0.5, typeProfile.getTypes()[0].getProbability(), DELTA); 180 Assert.assertEquals(0.5, typeProfile.getTypes()[1].getProbability(), DELTA); 181 182 resetProfile(testSnippet); 183 typeProfile = info.getTypeProfile(bci); 184 Assert.assertNull(typeProfile); 185 } 186 187 public ProfilingInfoTest() { 188 // These tests are explicitly testing the profiling behavior of the 189 // interpreter. C1-based profiling differs slightly and when -Xcomp 190 // is present, profiles will be created by C1 compiled code, not the 191 // interpreter. 192 Assume.assumeTrue(!SubprocessUtil.getVMCommandLine().contains("-Xcomp")); 193 } 194 195 @Test 196 public void testExceptionSeen() { 197 // NullPointerException 198 ProfilingInfo info = profile("nullPointerExceptionSnippet", 5); 199 Assert.assertEquals(TriState.FALSE, info.getExceptionSeen(1)); 200 201 info = profile("nullPointerExceptionSnippet", (Object) null); 202 Assert.assertEquals(TriState.TRUE, info.getExceptionSeen(1)); 203 204 resetProfile("nullPointerExceptionSnippet"); 205 Assert.assertEquals(TriState.FALSE, info.getExceptionSeen(1)); 206 207 // ArrayOutOfBoundsException 208 info = profile("arrayIndexOutOfBoundsExceptionSnippet", new int[1]); 209 Assert.assertEquals(TriState.FALSE, info.getExceptionSeen(2)); 210 211 info = profile("arrayIndexOutOfBoundsExceptionSnippet", new int[0]); 212 Assert.assertEquals(TriState.TRUE, info.getExceptionSeen(2)); |