xref: /aosp_15_r20/art/test/102-concurrent-gc/src/Main.java (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2012 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 import java.util.Random;
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker public class Main {
20*795d594fSAndroid Build Coastguard Worker     private static final int buckets = 16 * 1024;
21*795d594fSAndroid Build Coastguard Worker     private static final int bufferSize = 1024;
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker     static class ByteContainer {
24*795d594fSAndroid Build Coastguard Worker         public byte[] bytes;
25*795d594fSAndroid Build Coastguard Worker     }
26*795d594fSAndroid Build Coastguard Worker 
main(String[] args)27*795d594fSAndroid Build Coastguard Worker     public static void main(String[] args) throws Exception {
28*795d594fSAndroid Build Coastguard Worker         ByteContainer[] l = new ByteContainer[buckets];
29*795d594fSAndroid Build Coastguard Worker 
30*795d594fSAndroid Build Coastguard Worker         for (int i = 0; i < buckets; ++i) {
31*795d594fSAndroid Build Coastguard Worker             l[i] = new ByteContainer();
32*795d594fSAndroid Build Coastguard Worker         }
33*795d594fSAndroid Build Coastguard Worker 
34*795d594fSAndroid Build Coastguard Worker         Random rnd = new Random(123456);
35*795d594fSAndroid Build Coastguard Worker         for (int i = 0; i < buckets / 256; ++i) {
36*795d594fSAndroid Build Coastguard Worker             int index = rnd.nextInt(buckets);
37*795d594fSAndroid Build Coastguard Worker             l[index].bytes = new byte[bufferSize];
38*795d594fSAndroid Build Coastguard Worker 
39*795d594fSAndroid Build Coastguard Worker             // Try to get GC to run if we can
40*795d594fSAndroid Build Coastguard Worker             Runtime.getRuntime().gc();
41*795d594fSAndroid Build Coastguard Worker 
42*795d594fSAndroid Build Coastguard Worker             // Shuffle the array to try cause the lost object problem:
43*795d594fSAndroid Build Coastguard Worker             // This problem occurs when an object is white, it may be
44*795d594fSAndroid Build Coastguard Worker             // only referenced from a white or grey object. If the white
45*795d594fSAndroid Build Coastguard Worker             // object is moved during a CMS to be a black object's field, it
46*795d594fSAndroid Build Coastguard Worker             // causes the moved object to not get marked. This can result in
47*795d594fSAndroid Build Coastguard Worker             // heap corruption. A typical way to address this issue is by
48*795d594fSAndroid Build Coastguard Worker             // having a card table.
49*795d594fSAndroid Build Coastguard Worker             // This aspect of the test is meant to ensure that card
50*795d594fSAndroid Build Coastguard Worker             // dirtying works and that we check the marked cards after
51*795d594fSAndroid Build Coastguard Worker             // marking.
52*795d594fSAndroid Build Coastguard Worker             // If these operations are not done, a segfault / failed assert
53*795d594fSAndroid Build Coastguard Worker             // should occur.
54*795d594fSAndroid Build Coastguard Worker             for (int j = 0; j < l.length; ++j) {
55*795d594fSAndroid Build Coastguard Worker                 int a = l.length - i - 1;
56*795d594fSAndroid Build Coastguard Worker                 int b = rnd.nextInt(a);
57*795d594fSAndroid Build Coastguard Worker                 byte[] temp = l[a].bytes;
58*795d594fSAndroid Build Coastguard Worker                 l[a].bytes = l[b].bytes;
59*795d594fSAndroid Build Coastguard Worker                 l[b].bytes = temp;
60*795d594fSAndroid Build Coastguard Worker             }
61*795d594fSAndroid Build Coastguard Worker         }
62*795d594fSAndroid Build Coastguard Worker         System.out.println("Test complete");
63*795d594fSAndroid Build Coastguard Worker     }
64*795d594fSAndroid Build Coastguard Worker }
65