comparison truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/MergeSpecializationsTest.java @ 21951:9c8c0937da41

Moving all sources into truffle subdirectory
author Jaroslav Tulach <jaroslav.tulach@oracle.com>
date Wed, 17 Jun 2015 10:58:08 +0200
parents graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/MergeSpecializationsTest.java@e773cc48d3e8
children dc83cc1f94f2
comparison
equal deleted inserted replaced
21950:2a5011c7e641 21951:9c8c0937da41
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.dsl.test;
24
25 import static com.oracle.truffle.api.dsl.test.TestHelper.*;
26 import static org.junit.Assert.*;
27
28 import java.util.*;
29 import java.util.concurrent.*;
30
31 import org.junit.*;
32
33 import com.oracle.truffle.api.dsl.*;
34 import com.oracle.truffle.api.dsl.internal.*;
35 import com.oracle.truffle.api.dsl.test.MergeSpecializationsTestFactory.TestCachedNodeFactory;
36 import com.oracle.truffle.api.dsl.test.MergeSpecializationsTestFactory.TestNodeFactory;
37 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode;
38 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
39 import com.oracle.truffle.api.nodes.*;
40
41 public class MergeSpecializationsTest {
42
43 private static final int THREADS = 50;
44
45 @NodeChild
46 @SuppressWarnings("unused")
47 abstract static class TestNode extends ValueNode {
48
49 @Specialization
50 int s1(int a) {
51 return 1;
52 }
53
54 @Specialization
55 int s2(long a) {
56 return 2;
57 }
58
59 @Specialization
60 int s3(double a) {
61 return 3;
62 }
63 }
64
65 @NodeChild
66 @SuppressWarnings("unused")
67 abstract static class TestCachedNode extends ValueNode {
68
69 @Specialization(guards = "a == cachedA", limit = "3")
70 int s1(int a, @Cached("a") int cachedA) {
71 return 1;
72 }
73
74 @Specialization
75 int s2(long a) {
76 return 2;
77 }
78
79 @Specialization
80 int s3(double a) {
81 return 3;
82 }
83 }
84
85 @Test
86 public void testMultithreadedMergeInOrder() {
87 for (int i = 0; i < 100; i++) {
88 multithreadedMerge(TestNodeFactory.getInstance(), new Executions(1, 1L << 32, 1.0), 1, 2, 3);
89 }
90 }
91
92 @Test
93 public void testMultithreadedMergeReverse() {
94 for (int i = 0; i < 100; i++) {
95 multithreadedMerge(TestNodeFactory.getInstance(), new Executions(1.0, 1L << 32, 1), 3, 2, 1);
96 }
97 }
98
99 @Test
100 public void testMultithreadedMergeCachedInOrder() {
101 for (int i = 0; i < 100; i++) {
102 multithreadedMerge(TestCachedNodeFactory.getInstance(), new Executions(1, 1L << 32, 1.0), 1, 2, 3);
103 }
104 }
105
106 @Test
107 public void testMultithreadedMergeCachedTwoEntries() {
108 for (int i = 0; i < 100; i++) {
109 multithreadedMerge(TestCachedNodeFactory.getInstance(), new Executions(1, 2, 1.0), 1, 1, 3);
110 }
111 }
112
113 @Test
114 public void testMultithreadedMergeCachedThreeEntries() {
115 for (int i = 0; i < 100; i++) {
116 multithreadedMerge(TestCachedNodeFactory.getInstance(), new Executions(1, 2, 3), 1, 1, 1);
117 }
118 }
119
120 private static <T extends ValueNode> void multithreadedMerge(NodeFactory<T> factory, final Executions executions, int... order) {
121 assertEquals(3, order.length);
122 final TestRootNode<T> node = createRoot(factory);
123
124 final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
125
126 final CountDownLatch beforeFirst = new CountDownLatch(1);
127 final CountDownLatch executedFirst = new CountDownLatch(THREADS);
128
129 final CountDownLatch beforeSecond = new CountDownLatch(1);
130 final CountDownLatch executedSecond = new CountDownLatch(THREADS);
131
132 final CountDownLatch beforeThird = new CountDownLatch(1);
133 final CountDownLatch executedThird = new CountDownLatch(THREADS);
134
135 Thread[] threads = new Thread[THREADS];
136 for (int i = 0; i < threads.length; i++) {
137 threads[i] = new Thread(new Runnable() {
138 public void run() {
139 threadsStarted.countDown();
140
141 MergeSpecializationsTest.await(beforeFirst);
142 executeWith(node, executions.firstValue);
143 executedFirst.countDown();
144
145 MergeSpecializationsTest.await(beforeSecond);
146 executeWith(node, executions.secondValue);
147 executedSecond.countDown();
148
149 MergeSpecializationsTest.await(beforeThird);
150 executeWith(node, executions.thirdValue);
151 executedThird.countDown();
152 }
153 });
154 threads[i].start();
155 }
156
157 final SpecializedNode gen = (SpecializedNode) node.getNode();
158
159 final SpecializationNode start0 = gen.getSpecializationNode();
160 assertEquals("UninitializedNode_", start0.getClass().getSimpleName());
161
162 await(threadsStarted);
163 beforeFirst.countDown();
164 await(executedFirst);
165
166 final SpecializationNode start1 = gen.getSpecializationNode();
167 assertEquals("S" + order[0] + "Node_", start1.getClass().getSimpleName());
168 assertEquals("UninitializedNode_", nthChild(1, start1).getClass().getSimpleName());
169
170 beforeSecond.countDown();
171 await(executedSecond);
172
173 final SpecializationNode start2 = gen.getSpecializationNode();
174 Arrays.sort(order, 0, 2);
175 assertEquals("PolymorphicNode_", start2.getClass().getSimpleName());
176 assertEquals("S" + order[0] + "Node_", nthChild(1, start2).getClass().getSimpleName());
177 assertEquals("S" + order[1] + "Node_", nthChild(2, start2).getClass().getSimpleName());
178 assertEquals("UninitializedNode_", nthChild(3, start2).getClass().getSimpleName());
179
180 beforeThird.countDown();
181 await(executedThird);
182
183 final SpecializationNode start3 = gen.getSpecializationNode();
184 Arrays.sort(order);
185 assertEquals("PolymorphicNode_", start3.getClass().getSimpleName());
186 assertEquals("S" + order[0] + "Node_", nthChild(1, start3).getClass().getSimpleName());
187 assertEquals("S" + order[1] + "Node_", nthChild(2, start3).getClass().getSimpleName());
188 assertEquals("S" + order[2] + "Node_", nthChild(3, start3).getClass().getSimpleName());
189 assertEquals("UninitializedNode_", nthChild(4, start3).getClass().getSimpleName());
190
191 for (Thread thread : threads) {
192 try {
193 thread.join();
194 } catch (InterruptedException e) {
195 fail("interrupted");
196 }
197 }
198 }
199
200 private static class Executions {
201 public final Object firstValue;
202 public final Object secondValue;
203 public final Object thirdValue;
204
205 public Executions(Object firstValue, Object secondValue, Object thirdValue) {
206 this.firstValue = firstValue;
207 this.secondValue = secondValue;
208 this.thirdValue = thirdValue;
209 }
210 }
211
212 private static void await(final CountDownLatch latch) {
213 try {
214 latch.await();
215 } catch (InterruptedException e) {
216 fail("interrupted");
217 }
218 }
219
220 private static Node firstChild(Node node) {
221 return node.getChildren().iterator().next();
222 }
223
224 private static Node nthChild(int n, Node node) {
225 if (n == 0) {
226 return node;
227 } else {
228 return nthChild(n - 1, firstChild(node));
229 }
230 }
231 }