xref: /aosp_15_r20/art/test/658-fp-read-barrier/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2017 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker public class Main {
18*795d594fSAndroid Build Coastguard Worker   static volatile boolean done = false;
19*795d594fSAndroid Build Coastguard Worker 
main(String[] args)20*795d594fSAndroid Build Coastguard Worker   public static void main(String[] args) {
21*795d594fSAndroid Build Coastguard Worker     // Run a thread for 30 seconds, allocating memory and triggering garbage
22*795d594fSAndroid Build Coastguard Worker     // collection.
23*795d594fSAndroid Build Coastguard Worker     // Time is limited to 30 seconds to not make this test too long. The test used
24*795d594fSAndroid Build Coastguard Worker     // to trigger the failure around 1 every 10 runs.
25*795d594fSAndroid Build Coastguard Worker     Thread t = new Thread() {
26*795d594fSAndroid Build Coastguard Worker       public void run() {
27*795d594fSAndroid Build Coastguard Worker         long time = System.currentTimeMillis();
28*795d594fSAndroid Build Coastguard Worker         while (System.currentTimeMillis() - time < 30000) {
29*795d594fSAndroid Build Coastguard Worker           for (int j = 0; j < 10000; j++) {
30*795d594fSAndroid Build Coastguard Worker             o = new Object[1000];
31*795d594fSAndroid Build Coastguard Worker           }
32*795d594fSAndroid Build Coastguard Worker           Runtime.getRuntime().gc();
33*795d594fSAndroid Build Coastguard Worker           Thread.yield();
34*795d594fSAndroid Build Coastguard Worker         }
35*795d594fSAndroid Build Coastguard Worker         Main.done = true;
36*795d594fSAndroid Build Coastguard Worker       }
37*795d594fSAndroid Build Coastguard Worker       Object o;
38*795d594fSAndroid Build Coastguard Worker     };
39*795d594fSAndroid Build Coastguard Worker     // Make the thread a daemon to quit early in case of an
40*795d594fSAndroid Build Coastguard Worker     // exception thrown below.
41*795d594fSAndroid Build Coastguard Worker     t.setDaemon(true);
42*795d594fSAndroid Build Coastguard Worker     t.start();
43*795d594fSAndroid Build Coastguard Worker 
44*795d594fSAndroid Build Coastguard Worker     // Run 'foo' as long as the test runs.
45*795d594fSAndroid Build Coastguard Worker     while (!done) {
46*795d594fSAndroid Build Coastguard Worker       double res = foo(staticMain);
47*795d594fSAndroid Build Coastguard Worker       if (res != 529.0) {
48*795d594fSAndroid Build Coastguard Worker         throw new Error("Unexpected result " + res);
49*795d594fSAndroid Build Coastguard Worker       }
50*795d594fSAndroid Build Coastguard Worker     }
51*795d594fSAndroid Build Coastguard Worker   }
52*795d594fSAndroid Build Coastguard Worker 
foo(Main main)53*795d594fSAndroid Build Coastguard Worker   public static double foo(Main main) {
54*795d594fSAndroid Build Coastguard Worker     // Use up all D registers on arm64.
55*795d594fSAndroid Build Coastguard Worker     double d1 = main.field1;
56*795d594fSAndroid Build Coastguard Worker     double d2 = main.field2;
57*795d594fSAndroid Build Coastguard Worker     double d3 = main.field3;
58*795d594fSAndroid Build Coastguard Worker     double d4 = main.field4;
59*795d594fSAndroid Build Coastguard Worker     double d5 = main.field5;
60*795d594fSAndroid Build Coastguard Worker     double d6 = main.field6;
61*795d594fSAndroid Build Coastguard Worker     double d7 = main.field7;
62*795d594fSAndroid Build Coastguard Worker     double d8 = main.field8;
63*795d594fSAndroid Build Coastguard Worker     double d9 = main.field9;
64*795d594fSAndroid Build Coastguard Worker     double d10 = main.field10;
65*795d594fSAndroid Build Coastguard Worker     double d11 = main.field11;
66*795d594fSAndroid Build Coastguard Worker     double d12 = main.field12;
67*795d594fSAndroid Build Coastguard Worker     double d13 = main.field13;
68*795d594fSAndroid Build Coastguard Worker     double d14 = main.field14;
69*795d594fSAndroid Build Coastguard Worker     double d15 = main.field15;
70*795d594fSAndroid Build Coastguard Worker     double d16 = main.field16;
71*795d594fSAndroid Build Coastguard Worker     double d17 = main.field17;
72*795d594fSAndroid Build Coastguard Worker     double d18 = main.field18;
73*795d594fSAndroid Build Coastguard Worker     double d19 = main.field19;
74*795d594fSAndroid Build Coastguard Worker     double d20 = main.field20;
75*795d594fSAndroid Build Coastguard Worker     double d21 = main.field21;
76*795d594fSAndroid Build Coastguard Worker     double d22 = main.field22;
77*795d594fSAndroid Build Coastguard Worker     double d23 = main.field23;
78*795d594fSAndroid Build Coastguard Worker     double d24 = main.field24;
79*795d594fSAndroid Build Coastguard Worker     double d25 = main.field25;
80*795d594fSAndroid Build Coastguard Worker     double d26 = main.field26;
81*795d594fSAndroid Build Coastguard Worker     double d27 = main.field27;
82*795d594fSAndroid Build Coastguard Worker     double d28 = main.field28;
83*795d594fSAndroid Build Coastguard Worker     double d29 = main.field29;
84*795d594fSAndroid Build Coastguard Worker     double d30 = main.field30;
85*795d594fSAndroid Build Coastguard Worker     double d31 = main.field31;
86*795d594fSAndroid Build Coastguard Worker     double d32 = main.field32;
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker     // Trigger a read barrier. This used to make the test trip on ARM64 as
89*795d594fSAndroid Build Coastguard Worker     // the read barrier stub used to not restore the D registers.
90*795d594fSAndroid Build Coastguard Worker     double p = main.objectField.field1;
91*795d594fSAndroid Build Coastguard Worker 
92*795d594fSAndroid Build Coastguard Worker     return p + d1 + d2 + d3 + d4 + d5 + d6 + d7 + d8 + d9 + d10 + d11 + d12 +
93*795d594fSAndroid Build Coastguard Worker         d13 + d14 + d15 + d16 + d17 + d18 + d19 + d20 + d21 + d22 + d23 + d24 +
94*795d594fSAndroid Build Coastguard Worker         d25 + d26 + d27 + d28 + d29 + d30 + d31 + d32;
95*795d594fSAndroid Build Coastguard Worker   }
96*795d594fSAndroid Build Coastguard Worker 
97*795d594fSAndroid Build Coastguard Worker   // Initialize objects here and not in 'main' to avoid having
98*795d594fSAndroid Build Coastguard Worker   // these objects in roots.
99*795d594fSAndroid Build Coastguard Worker   public static Main staticMain = new Main();
100*795d594fSAndroid Build Coastguard Worker   static {
101*795d594fSAndroid Build Coastguard Worker     staticMain.objectField = new Main();
102*795d594fSAndroid Build Coastguard Worker   }
103*795d594fSAndroid Build Coastguard Worker 
104*795d594fSAndroid Build Coastguard Worker   public Main objectField;
105*795d594fSAndroid Build Coastguard Worker 
106*795d594fSAndroid Build Coastguard Worker   public double field1 = 1.0;
107*795d594fSAndroid Build Coastguard Worker   public double field2 = 2.0;
108*795d594fSAndroid Build Coastguard Worker   public double field3 = 3.0;
109*795d594fSAndroid Build Coastguard Worker   public double field4 = 4.0;
110*795d594fSAndroid Build Coastguard Worker   public double field5 = 5.0;
111*795d594fSAndroid Build Coastguard Worker   public double field6 = 6.0;
112*795d594fSAndroid Build Coastguard Worker   public double field7 = 7.0;
113*795d594fSAndroid Build Coastguard Worker   public double field8 = 8.0;
114*795d594fSAndroid Build Coastguard Worker   public double field9 = 9.0;
115*795d594fSAndroid Build Coastguard Worker   public double field10 = 10.0;
116*795d594fSAndroid Build Coastguard Worker   public double field11 = 11.0;
117*795d594fSAndroid Build Coastguard Worker   public double field12 = 12.0;
118*795d594fSAndroid Build Coastguard Worker   public double field13 = 13.0;
119*795d594fSAndroid Build Coastguard Worker   public double field14 = 14.0;
120*795d594fSAndroid Build Coastguard Worker   public double field15 = 15.0;
121*795d594fSAndroid Build Coastguard Worker   public double field16 = 16.0;
122*795d594fSAndroid Build Coastguard Worker   public double field17 = 17.0;
123*795d594fSAndroid Build Coastguard Worker   public double field18 = 18.0;
124*795d594fSAndroid Build Coastguard Worker   public double field19 = 19.0;
125*795d594fSAndroid Build Coastguard Worker   public double field20 = 20.0;
126*795d594fSAndroid Build Coastguard Worker   public double field21 = 21.0;
127*795d594fSAndroid Build Coastguard Worker   public double field22 = 22.0;
128*795d594fSAndroid Build Coastguard Worker   public double field23 = 23.0;
129*795d594fSAndroid Build Coastguard Worker   public double field24 = 24.0;
130*795d594fSAndroid Build Coastguard Worker   public double field25 = 25.0;
131*795d594fSAndroid Build Coastguard Worker   public double field26 = 26.0;
132*795d594fSAndroid Build Coastguard Worker   public double field27 = 27.0;
133*795d594fSAndroid Build Coastguard Worker   public double field28 = 28.0;
134*795d594fSAndroid Build Coastguard Worker   public double field29 = 29.0;
135*795d594fSAndroid Build Coastguard Worker   public double field30 = 30.0;
136*795d594fSAndroid Build Coastguard Worker   public double field31 = 31.0;
137*795d594fSAndroid Build Coastguard Worker   public double field32 = 32.0;
138*795d594fSAndroid Build Coastguard Worker }
139