1e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson/*
2e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * Copyright (C) 2011 The Android Open Source Project
3e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson *
4e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * Licensed under the Apache License, Version 2.0 (the "License");
5e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * you may not use this file except in compliance with the License.
6e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * You may obtain a copy of the License at
7e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson *
8e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson *      http://www.apache.org/licenses/LICENSE-2.0
9e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson *
10e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * Unless required by applicable law or agreed to in writing, software
11e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * distributed under the License is distributed on an "AS IS" BASIS,
12e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * See the License for the specific language governing permissions and
14e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * limitations under the License.
15e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson */
16e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson
17e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson/**
18e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * Running concurrent gc and doing some System.arraycopy
19e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * Several threads is created in order to increase the probability
20e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * of thread switches at critical points. Without creating several
21e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * threads the test case usually passed even when there were bugs.
22e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * Size of array and amount of garbage created is based on experimental
23e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * numbers and is a tradeoff between time that the test takes when
24e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson * it succeeds and the probability that the test discovers a problem.
25e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson */
26e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlssonpublic class Main {
27e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson    public static void main(String args[]) {
28e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        new ObjectCreatorThread(true).start();
29e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        new ObjectCreatorThread(false).start();
30e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        new ObjectCreatorThread(false).start();
31e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson    }
32e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson
33e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson    static class ObjectCreatorThread extends Thread {
34e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        boolean mDoLog;
35e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        public ObjectCreatorThread(boolean doLog) {
36e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            mDoLog = doLog;
37e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        }
38e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson
39e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        @Override
40e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        public void run() {
41e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            new Main().stressArray(mDoLog);
42e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        }
43e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson    }
44e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson
45e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson    Object [] array = new Object[10000];
46e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson
47e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson    void stressArray(boolean doLog) {
48e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        // We want many references in the array
49e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        // We also want elements close to each other to have large
50e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        // diff in address so lets skip every 2:nd address so it is null
51e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        if (doLog) {
52e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            System.out.println("Initializing...");
53e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        }
54e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        for (int i = 0; i < array.length; i+=2) {
55e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            array[i] = new String("Creating some garbage" + i);
56e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        }
57e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson
58e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        if (doLog) {
59e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            System.out.println("Starting the test");
60e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        }
61e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson
62e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        for (int j = 0; j < array.length; j++) {
63e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            Object obj = array[array.length - 1];
64e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            System.arraycopy(array, 0, array, 1, array.length - 1);
65e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            array[0] = obj;
66e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            new String("Creating some garbage" + Math.random());
67e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            new String("Creating some garbage" + Math.random());
68e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            new String("Creating some garbage" + Math.random());
69e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            new String("Creating some garbage" + Math.random());
70e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        }
71e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson
72e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        for (int j = 0; j < array.length; j++) {
73e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            Object obj = array[0];
74e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            System.arraycopy(array, 1, array, 0, array.length - 1);
75e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            array[array.length - 1] = obj;
76e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            new String("Creating some garbage" + Math.random());
77e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            new String("Creating some garbage" + Math.random());
78e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            new String("Creating some garbage" + Math.random());
79e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            new String("Creating some garbage" + Math.random());
80e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        }
81e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson
82e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        if (doLog) {
83e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson            System.out.println("Test OK");
84e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson        }
85e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson    }
86e0e29754397bc0447d77d8cc82009d83ffb01208Johannes Carlsson}
87