1/*
2 * Copyright (c) 2011, 2018, 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
24package vm.mlvm.indy.stress.java.loopsAndThreads;
25
26import java.dyn.CallSite;
27import java.dyn.InvokeDynamic;
28import java.dyn.Linkage;
29import java.dyn.MethodHandles;
30import java.dyn.MethodType;
31import java.util.concurrent.atomic.AtomicInteger;
32import java.util.concurrent.atomic.AtomicLong;
33
34import vm.mlvm.share.MlvmTest;
35import vm.mlvm.share.MultiThreadedTest;
36
37public class Test extends MultiThreadedTest {
38
39    private static final int THREAD_COUNT = 100;
40    private static final int CYCLES_COUNT = 10000; // Provoke compiler
41    private static final long EXPECTED_TARGET_CALLS = 100L * THREAD_COUNT * CYCLES_COUNT;
42
43    private AtomicLong _counter = new AtomicLong();
44
45    static {
46        Linkage.registerBootstrapMethod(Test.class, "bootstrap");
47    }
48
49    public Test() {
50        super(THREAD_COUNT);
51    }
52
53    public static CallSite bootstrap(Class<?> c, String name, MethodType mt) {
54        getLog().trace(0, "Class " + c + "; method name = " + name + "; method type = " + mt);
55        CallSite cs = new CallSite(c, name, mt);
56        cs.setTarget(MethodHandles.publicLookup().findVirtual(
57                Test.class, "target", MethodType.methodType(Object.class, String.class, int.class)));
58        return cs;
59    }
60
61    public Object target(String s, int i) {
62        _counter.incrementAndGet();
63        return null;
64    }
65
66    @Override
67    public boolean runThread() throws Throwable {
68        final Test x = this;
69        for ( int i = 0; i < CYCLES_COUNT; i++ ) {
70            InvokeDynamic.greet(x, "world", 123);
71            InvokeDynamic.greet(x, "mundus", 456);
72            InvokeDynamic.greet(x, "kosmos", 789);
73            InvokeDynamic.greet(x, "world", 123);
74            InvokeDynamic.greet(x, "mundus", 456);
75            InvokeDynamic.greet(x, "kosmos", 789);
76            InvokeDynamic.greet(x, "world", 123);
77            InvokeDynamic.greet(x, "mundus", 456);
78            InvokeDynamic.greet(x, "kosmos", 789);
79            InvokeDynamic.greet(x, "mundus", 123);
80
81            InvokeDynamic.greet(x, "world", 123);
82            InvokeDynamic.greet(x, "mundus", 456);
83            InvokeDynamic.greet(x, "kosmos", 789);
84            InvokeDynamic.greet(x, "world", 123);
85            InvokeDynamic.greet(x, "mundus", 456);
86            InvokeDynamic.greet(x, "kosmos", 789);
87            InvokeDynamic.greet(x, "world", 123);
88            InvokeDynamic.greet(x, "mundus", 456);
89            InvokeDynamic.greet(x, "kosmos", 789);
90            InvokeDynamic.greet(x, "mundus", 123);
91
92            InvokeDynamic.greet(x, "world", 123);
93            InvokeDynamic.greet(x, "mundus", 456);
94            InvokeDynamic.greet(x, "kosmos", 789);
95            InvokeDynamic.greet(x, "world", 123);
96            InvokeDynamic.greet(x, "mundus", 456);
97            InvokeDynamic.greet(x, "kosmos", 789);
98            InvokeDynamic.greet(x, "world", 123);
99            InvokeDynamic.greet(x, "mundus", 456);
100            InvokeDynamic.greet(x, "kosmos", 789);
101            InvokeDynamic.greet(x, "mundus", 123);
102
103            InvokeDynamic.greet(x, "world", 123);
104            InvokeDynamic.greet(x, "mundus", 456);
105            InvokeDynamic.greet(x, "kosmos", 789);
106            InvokeDynamic.greet(x, "world", 123);
107            InvokeDynamic.greet(x, "mundus", 456);
108            InvokeDynamic.greet(x, "kosmos", 789);
109            InvokeDynamic.greet(x, "world", 123);
110            InvokeDynamic.greet(x, "mundus", 456);
111            InvokeDynamic.greet(x, "kosmos", 789);
112            InvokeDynamic.greet(x, "mundus", 123);
113
114            InvokeDynamic.greet(x, "world", 123);
115            InvokeDynamic.greet(x, "mundus", 456);
116            InvokeDynamic.greet(x, "kosmos", 789);
117            InvokeDynamic.greet(x, "world", 123);
118            InvokeDynamic.greet(x, "mundus", 456);
119            InvokeDynamic.greet(x, "kosmos", 789);
120            InvokeDynamic.greet(x, "world", 123);
121            InvokeDynamic.greet(x, "mundus", 456);
122            InvokeDynamic.greet(x, "kosmos", 789);
123            InvokeDynamic.greet(x, "mundus", 123);
124
125            InvokeDynamic.greet(x, "world", 123);
126            InvokeDynamic.greet(x, "mundus", 456);
127            InvokeDynamic.greet(x, "kosmos", 789);
128            InvokeDynamic.greet(x, "world", 123);
129            InvokeDynamic.greet(x, "mundus", 456);
130            InvokeDynamic.greet(x, "kosmos", 789);
131            InvokeDynamic.greet(x, "world", 123);
132            InvokeDynamic.greet(x, "mundus", 456);
133            InvokeDynamic.greet(x, "kosmos", 789);
134            InvokeDynamic.greet(x, "mundus", 123);
135
136            InvokeDynamic.greet(x, "world", 123);
137            InvokeDynamic.greet(x, "mundus", 456);
138            InvokeDynamic.greet(x, "kosmos", 789);
139            InvokeDynamic.greet(x, "world", 123);
140            InvokeDynamic.greet(x, "mundus", 456);
141            InvokeDynamic.greet(x, "kosmos", 789);
142            InvokeDynamic.greet(x, "world", 123);
143            InvokeDynamic.greet(x, "mundus", 456);
144            InvokeDynamic.greet(x, "kosmos", 789);
145            InvokeDynamic.greet(x, "mundus", 123);
146
147            InvokeDynamic.greet(x, "world", 123);
148            InvokeDynamic.greet(x, "mundus", 456);
149            InvokeDynamic.greet(x, "kosmos", 789);
150            InvokeDynamic.greet(x, "world", 123);
151            InvokeDynamic.greet(x, "mundus", 456);
152            InvokeDynamic.greet(x, "kosmos", 789);
153            InvokeDynamic.greet(x, "world", 123);
154            InvokeDynamic.greet(x, "mundus", 456);
155            InvokeDynamic.greet(x, "kosmos", 789);
156            InvokeDynamic.greet(x, "mundus", 123);
157
158            InvokeDynamic.greet(x, "world", 123);
159            InvokeDynamic.greet(x, "mundus", 456);
160            InvokeDynamic.greet(x, "kosmos", 789);
161            InvokeDynamic.greet(x, "world", 123);
162            InvokeDynamic.greet(x, "mundus", 456);
163            InvokeDynamic.greet(x, "kosmos", 789);
164            InvokeDynamic.greet(x, "world", 123);
165            InvokeDynamic.greet(x, "mundus", 456);
166            InvokeDynamic.greet(x, "kosmos", 789);
167            InvokeDynamic.greet(x, "mundus", 123);
168
169            InvokeDynamic.greet(x, "world", 123);
170            InvokeDynamic.greet(x, "mundus", 456);
171            InvokeDynamic.greet(x, "kosmos", 789);
172            InvokeDynamic.greet(x, "world", 123);
173            InvokeDynamic.greet(x, "mundus", 456);
174            InvokeDynamic.greet(x, "kosmos", 789);
175            InvokeDynamic.greet(x, "world", 123);
176            InvokeDynamic.greet(x, "mundus", 456);
177            InvokeDynamic.greet(x, "kosmos", 789);
178            InvokeDynamic.greet(x, "mundus", 123);
179        }
180
181        return true;
182    }
183
184    protected boolean tearDown() throws Throwable {
185        getLog().display("Target was called " + _counter + " times of " + EXPECTED_TARGET_CALLS);
186        return _counter.get() == EXPECTED_TARGET_CALLS;
187    }
188
189    public static void main(String[] args) { MlvmTest.launch(args); }
190}
191