1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.cts.compilation;
18 
19 import android.app.Activity;
20 import android.os.AsyncTask;
21 
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.Random;
26 
27 /**
28  * A simple activity which can be subjected to (dex to native) compilation.
29  *
30  * If you change this code, you need to regenerate APK and profile using the
31  * the new code - see instructions in {@code assets/README.txt}.
32  */
33 public class CompilationTargetActivity extends Activity {
34 
35     private AsyncTask<Integer, String, Void> mTask;
36 
37     @Override
onResume()38     protected void onResume() {
39         super.onResume();
40         setTitle("Starting...");
41         mTask = new AsyncTask<Integer, String, Void>() {
42             @Override
43             protected Void doInBackground(Integer... params) {
44                 int numValues = params[0];
45                 int numIter = params[1];
46                 for (int i = 0; i < numIter; i++) {
47                     if (Thread.interrupted()) {
48                         break;
49                     }
50                     publishProgress("Step " + (i+1) + " of " + numIter);
51                     List<Integer> values = makeValues(numValues);
52                     Collections.shuffle(values);
53                     Collections.sort(values);
54                 }
55                 return null;
56             }
57 
58             @Override
59             protected void onProgressUpdate(String... values) {
60                 setTitle(values[0]);
61             }
62 
63             @Override
64             protected void onPostExecute(Void aVoid) {
65                 setTitle("Done");
66             }
67         };
68         mTask.execute(1024, 100 * 1000);
69     }
70 
71     @Override
onPause()72     protected void onPause() {
73         mTask.cancel(/* mayInterruptIfRunning */ true);
74         mTask = null;
75         super.onPause();
76     }
77 
makeValues(int numValues)78     private List<Integer> makeValues(int numValues) {
79         List<Integer> result = new ArrayList<>();
80         Random random = new Random();
81         for (int i = 0; i < numValues; i++) {
82             int v = dispatch(random.nextInt());
83             result.add(v);
84         }
85         return result;
86     }
87 
88     /**
89      * Dispatches to a bunch of simple methods because JIT profiles are only generated for
90      * apps with enough methods (10, as of May 2016).
91      */
dispatch(int i)92     private int dispatch(int i) {
93         int v = Math.abs(i % 100);
94         switch (v) {
95             case 0: return m0();
96             case 1: return m1();
97             case 2: return m2();
98             case 3: return m3();
99             case 4: return m4();
100             case 5: return m5();
101             case 6: return m6();
102             case 7: return m7();
103             case 8: return m8();
104             case 9: return m9();
105             case 10: return m10();
106             case 11: return m11();
107             case 12: return m12();
108             case 13: return m13();
109             case 14: return m14();
110             case 15: return m15();
111             case 16: return m16();
112             case 17: return m17();
113             case 18: return m18();
114             case 19: return m19();
115             case 20: return m20();
116             case 21: return m21();
117             case 22: return m22();
118             case 23: return m23();
119             case 24: return m24();
120             case 25: return m25();
121             case 26: return m26();
122             case 27: return m27();
123             case 28: return m28();
124             case 29: return m29();
125             case 30: return m30();
126             case 31: return m31();
127             case 32: return m32();
128             case 33: return m33();
129             case 34: return m34();
130             case 35: return m35();
131             case 36: return m36();
132             case 37: return m37();
133             case 38: return m38();
134             case 39: return m39();
135             case 40: return m40();
136             case 41: return m41();
137             case 42: return m42();
138             case 43: return m43();
139             case 44: return m44();
140             case 45: return m45();
141             case 46: return m46();
142             case 47: return m47();
143             case 48: return m48();
144             case 49: return m49();
145             case 50: return m50();
146             case 51: return m51();
147             case 52: return m52();
148             case 53: return m53();
149             case 54: return m54();
150             case 55: return m55();
151             case 56: return m56();
152             case 57: return m57();
153             case 58: return m58();
154             case 59: return m59();
155             case 60: return m60();
156             case 61: return m61();
157             case 62: return m62();
158             case 63: return m63();
159             case 64: return m64();
160             case 65: return m65();
161             case 66: return m66();
162             case 67: return m67();
163             case 68: return m68();
164             case 69: return m69();
165             case 70: return m70();
166             case 71: return m71();
167             case 72: return m72();
168             case 73: return m73();
169             case 74: return m74();
170             case 75: return m75();
171             case 76: return m76();
172             case 77: return m77();
173             case 78: return m78();
174             case 79: return m79();
175             case 80: return m80();
176             case 81: return m81();
177             case 82: return m82();
178             case 83: return m83();
179             case 84: return m84();
180             case 85: return m85();
181             case 86: return m86();
182             case 87: return m87();
183             case 88: return m88();
184             case 89: return m89();
185             case 90: return m90();
186             case 91: return m91();
187             case 92: return m92();
188             case 93: return m93();
189             case 94: return m94();
190             case 95: return m95();
191             case 96: return m96();
192             case 97: return m97();
193             case 98: return m98();
194             case 99: return m99();
195             default: throw new AssertionError(v + " out of bounds");
196         }
197     }
198 
m0()199     public int m0() { return new Random(0).nextInt(); }
m1()200     public int m1() { return new Random(1).nextInt(); }
m2()201     public int m2() { return new Random(2).nextInt(); }
m3()202     public int m3() { return new Random(3).nextInt(); }
m4()203     public int m4() { return new Random(4).nextInt(); }
m5()204     public int m5() { return new Random(5).nextInt(); }
m6()205     public int m6() { return new Random(6).nextInt(); }
m7()206     public int m7() { return new Random(7).nextInt(); }
m8()207     public int m8() { return new Random(8).nextInt(); }
m9()208     public int m9() { return new Random(9).nextInt(); }
m10()209     public int m10() { return new Random(10).nextInt(); }
m11()210     public int m11() { return new Random(11).nextInt(); }
m12()211     public int m12() { return new Random(12).nextInt(); }
m13()212     public int m13() { return new Random(13).nextInt(); }
m14()213     public int m14() { return new Random(14).nextInt(); }
m15()214     public int m15() { return new Random(15).nextInt(); }
m16()215     public int m16() { return new Random(16).nextInt(); }
m17()216     public int m17() { return new Random(17).nextInt(); }
m18()217     public int m18() { return new Random(18).nextInt(); }
m19()218     public int m19() { return new Random(19).nextInt(); }
m20()219     public int m20() { return new Random(20).nextInt(); }
m21()220     public int m21() { return new Random(21).nextInt(); }
m22()221     public int m22() { return new Random(22).nextInt(); }
m23()222     public int m23() { return new Random(23).nextInt(); }
m24()223     public int m24() { return new Random(24).nextInt(); }
m25()224     public int m25() { return new Random(25).nextInt(); }
m26()225     public int m26() { return new Random(26).nextInt(); }
m27()226     public int m27() { return new Random(27).nextInt(); }
m28()227     public int m28() { return new Random(28).nextInt(); }
m29()228     public int m29() { return new Random(29).nextInt(); }
m30()229     public int m30() { return new Random(30).nextInt(); }
m31()230     public int m31() { return new Random(31).nextInt(); }
m32()231     public int m32() { return new Random(32).nextInt(); }
m33()232     public int m33() { return new Random(33).nextInt(); }
m34()233     public int m34() { return new Random(34).nextInt(); }
m35()234     public int m35() { return new Random(35).nextInt(); }
m36()235     public int m36() { return new Random(36).nextInt(); }
m37()236     public int m37() { return new Random(37).nextInt(); }
m38()237     public int m38() { return new Random(38).nextInt(); }
m39()238     public int m39() { return new Random(39).nextInt(); }
m40()239     public int m40() { return new Random(40).nextInt(); }
m41()240     public int m41() { return new Random(41).nextInt(); }
m42()241     public int m42() { return new Random(42).nextInt(); }
m43()242     public int m43() { return new Random(43).nextInt(); }
m44()243     public int m44() { return new Random(44).nextInt(); }
m45()244     public int m45() { return new Random(45).nextInt(); }
m46()245     public int m46() { return new Random(46).nextInt(); }
m47()246     public int m47() { return new Random(47).nextInt(); }
m48()247     public int m48() { return new Random(48).nextInt(); }
m49()248     public int m49() { return new Random(49).nextInt(); }
m50()249     public int m50() { return new Random(50).nextInt(); }
m51()250     public int m51() { return new Random(51).nextInt(); }
m52()251     public int m52() { return new Random(52).nextInt(); }
m53()252     public int m53() { return new Random(53).nextInt(); }
m54()253     public int m54() { return new Random(54).nextInt(); }
m55()254     public int m55() { return new Random(55).nextInt(); }
m56()255     public int m56() { return new Random(56).nextInt(); }
m57()256     public int m57() { return new Random(57).nextInt(); }
m58()257     public int m58() { return new Random(58).nextInt(); }
m59()258     public int m59() { return new Random(59).nextInt(); }
m60()259     public int m60() { return new Random(60).nextInt(); }
m61()260     public int m61() { return new Random(61).nextInt(); }
m62()261     public int m62() { return new Random(62).nextInt(); }
m63()262     public int m63() { return new Random(63).nextInt(); }
m64()263     public int m64() { return new Random(64).nextInt(); }
m65()264     public int m65() { return new Random(65).nextInt(); }
m66()265     public int m66() { return new Random(66).nextInt(); }
m67()266     public int m67() { return new Random(67).nextInt(); }
m68()267     public int m68() { return new Random(68).nextInt(); }
m69()268     public int m69() { return new Random(69).nextInt(); }
m70()269     public int m70() { return new Random(70).nextInt(); }
m71()270     public int m71() { return new Random(71).nextInt(); }
m72()271     public int m72() { return new Random(72).nextInt(); }
m73()272     public int m73() { return new Random(73).nextInt(); }
m74()273     public int m74() { return new Random(74).nextInt(); }
m75()274     public int m75() { return new Random(75).nextInt(); }
m76()275     public int m76() { return new Random(76).nextInt(); }
m77()276     public int m77() { return new Random(77).nextInt(); }
m78()277     public int m78() { return new Random(78).nextInt(); }
m79()278     public int m79() { return new Random(79).nextInt(); }
m80()279     public int m80() { return new Random(80).nextInt(); }
m81()280     public int m81() { return new Random(81).nextInt(); }
m82()281     public int m82() { return new Random(82).nextInt(); }
m83()282     public int m83() { return new Random(83).nextInt(); }
m84()283     public int m84() { return new Random(84).nextInt(); }
m85()284     public int m85() { return new Random(85).nextInt(); }
m86()285     public int m86() { return new Random(86).nextInt(); }
m87()286     public int m87() { return new Random(87).nextInt(); }
m88()287     public int m88() { return new Random(88).nextInt(); }
m89()288     public int m89() { return new Random(89).nextInt(); }
m90()289     public int m90() { return new Random(90).nextInt(); }
m91()290     public int m91() { return new Random(91).nextInt(); }
m92()291     public int m92() { return new Random(92).nextInt(); }
m93()292     public int m93() { return new Random(93).nextInt(); }
m94()293     public int m94() { return new Random(94).nextInt(); }
m95()294     public int m95() { return new Random(95).nextInt(); }
m96()295     public int m96() { return new Random(96).nextInt(); }
m97()296     public int m97() { return new Random(97).nextInt(); }
m98()297     public int m98() { return new Random(98).nextInt(); }
m99()298     public int m99() { return new Random(99).nextInt(); }
299 
300 }
301