Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/profiles/LazyProfileLoadingTest.java @ 22501:a63bda98cfdb
Extract profiles into separate package. Add isProfilingEnabled in TruffleRuntime to disable profiling in the default runtime; Add low overhead profiles for primitives; Add LoopConditionProfile; Profile footprint/threadsafety improvements; Make toString implementations more consistent; Greatly enhanced javadoc documentation for profiles; Deprecate old profiles
author | Christian Humer <christian.humer@oracle.com> |
---|---|
date | Wed, 16 Dec 2015 16:38:13 +0100 |
parents | |
children | 4ba1aa33fda4 |
comparison
equal
deleted
inserted
replaced
22500:fbe1eb7b4172 | 22501:a63bda98cfdb |
---|---|
1 /* | |
2 * Copyright (c) 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. | |
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.truffle.api.profiles; | |
24 | |
25 import java.lang.reflect.InvocationTargetException; | |
26 import java.lang.reflect.Method; | |
27 import java.net.URLClassLoader; | |
28 | |
29 import org.junit.Assert; | |
30 import org.junit.Test; | |
31 import org.junit.runner.RunWith; | |
32 import org.junit.runners.BlockJUnit4ClassRunner; | |
33 import org.junit.runners.model.InitializationError; | |
34 | |
35 import com.oracle.truffle.api.Truffle; | |
36 import com.oracle.truffle.api.profiles.LazyProfileLoadingTest.SeparateClassloaderTestRunner; | |
37 | |
38 @RunWith(SeparateClassloaderTestRunner.class) | |
39 public class LazyProfileLoadingTest { | |
40 | |
41 @Test | |
42 public void testLazyLoading() { | |
43 IntValueProfile.createIdentityProfile(); | |
44 assertDefaultProfile(IntValueProfile.class); | |
45 | |
46 DoubleValueProfile.createRawIdentityProfile(); | |
47 assertDefaultProfile(DoubleValueProfile.class); | |
48 | |
49 LongValueProfile.createIdentityProfile(); | |
50 assertDefaultProfile(LongValueProfile.class); | |
51 | |
52 FloatValueProfile.createRawIdentityProfile(); | |
53 assertDefaultProfile(FloatValueProfile.class); | |
54 | |
55 ByteValueProfile.createIdentityProfile(); | |
56 assertDefaultProfile(ByteValueProfile.class); | |
57 | |
58 LoopConditionProfile.createCountingProfile(); | |
59 assertDefaultProfile(LoopConditionProfile.class); | |
60 | |
61 BranchProfile.create(); | |
62 assertDefaultProfile(BranchProfile.class); | |
63 | |
64 PrimitiveValueProfile.createEqualityProfile(); | |
65 assertDefaultProfile(PrimitiveValueProfile.class); | |
66 | |
67 ConditionProfile.createBinaryProfile(); | |
68 assertLoaded(ConditionProfile.class.getName(), "Binary", "Disabled"); | |
69 | |
70 ConditionProfile.createCountingProfile(); | |
71 assertLoaded(ConditionProfile.class.getName(), "Counting", "Disabled"); | |
72 | |
73 ValueProfile.createClassProfile(); | |
74 assertLoaded(ValueProfile.class.getName(), "ExactClass", "Disabled"); | |
75 | |
76 ValueProfile.createIdentityProfile(); | |
77 assertLoaded(ValueProfile.class.getName(), "Identity", "Disabled"); | |
78 } | |
79 | |
80 private void assertDefaultProfile(Class<?> clazz) { | |
81 assertLoaded(clazz.getName(), "Enabled", "Disabled"); | |
82 } | |
83 | |
84 private void assertLoaded(String className, String posInnerClass, String negInnerClass) { | |
85 String enabledClass = className + "$" + posInnerClass; | |
86 String disabledClass = className + "$" + negInnerClass; | |
87 if (Truffle.getRuntime().isProfilingEnabled()) { | |
88 Assert.assertFalse(isLoaded(disabledClass)); | |
89 Assert.assertTrue(isLoaded(enabledClass)); | |
90 } else { | |
91 Assert.assertFalse(isLoaded(enabledClass)); | |
92 Assert.assertTrue(isLoaded(disabledClass)); | |
93 } | |
94 } | |
95 | |
96 private boolean isLoaded(String className) { | |
97 ClassLoader classLoader = getClass().getClassLoader(); | |
98 Method m; | |
99 try { | |
100 m = ClassLoader.class.getDeclaredMethod("findLoadedClass", String.class); | |
101 m.setAccessible(true); | |
102 return m.invoke(classLoader, className) != null; | |
103 } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | |
104 throw new RuntimeException(e); | |
105 } | |
106 } | |
107 | |
108 public static class SeparateClassloaderTestRunner extends BlockJUnit4ClassRunner { | |
109 | |
110 public SeparateClassloaderTestRunner(Class<?> clazz) throws InitializationError { | |
111 super(getFromTestClassloader(clazz)); | |
112 } | |
113 | |
114 private static Class<?> getFromTestClassloader(Class<?> clazz) throws InitializationError { | |
115 try { | |
116 ClassLoader testClassLoader = new TestClassLoader(); | |
117 return Class.forName(clazz.getName(), true, testClassLoader); | |
118 } catch (ClassNotFoundException e) { | |
119 throw new InitializationError(e); | |
120 } | |
121 } | |
122 | |
123 public static class TestClassLoader extends URLClassLoader { | |
124 public TestClassLoader() { | |
125 super(((URLClassLoader) getSystemClassLoader()).getURLs()); | |
126 } | |
127 | |
128 @Override | |
129 public Class<?> loadClass(String name) throws ClassNotFoundException { | |
130 if (name.startsWith(Profile.class.getPackage().getName())) { | |
131 return super.findClass(name); | |
132 } | |
133 return super.loadClass(name); | |
134 } | |
135 } | |
136 } | |
137 | |
138 } |