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