1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2011 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 /** 18*795d594fSAndroid Build Coastguard Worker * Running concurrent gc and doing some System.arraycopy 19*795d594fSAndroid Build Coastguard Worker * Several threads is created in order to increase the probability 20*795d594fSAndroid Build Coastguard Worker * of thread switches at critical points. Without creating several 21*795d594fSAndroid Build Coastguard Worker * threads the test case usually passed even when there were bugs. 22*795d594fSAndroid Build Coastguard Worker * Size of array and amount of garbage created is based on experimental 23*795d594fSAndroid Build Coastguard Worker * numbers and is a tradeoff between time that the test takes when 24*795d594fSAndroid Build Coastguard Worker * it succeeds and the probability that the test discovers a problem. 25*795d594fSAndroid Build Coastguard Worker */ 26*795d594fSAndroid Build Coastguard Worker public class Main { main(String args[])27*795d594fSAndroid Build Coastguard Worker public static void main(String args[]) { 28*795d594fSAndroid Build Coastguard Worker new ObjectCreatorThread(true).start(); 29*795d594fSAndroid Build Coastguard Worker new ObjectCreatorThread(false).start(); 30*795d594fSAndroid Build Coastguard Worker new ObjectCreatorThread(false).start(); 31*795d594fSAndroid Build Coastguard Worker } 32*795d594fSAndroid Build Coastguard Worker 33*795d594fSAndroid Build Coastguard Worker static class ObjectCreatorThread extends Thread { 34*795d594fSAndroid Build Coastguard Worker boolean mDoLog; ObjectCreatorThread(boolean doLog)35*795d594fSAndroid Build Coastguard Worker public ObjectCreatorThread(boolean doLog) { 36*795d594fSAndroid Build Coastguard Worker mDoLog = doLog; 37*795d594fSAndroid Build Coastguard Worker } 38*795d594fSAndroid Build Coastguard Worker 39*795d594fSAndroid Build Coastguard Worker @Override run()40*795d594fSAndroid Build Coastguard Worker public void run() { 41*795d594fSAndroid Build Coastguard Worker new Main().stressArray(mDoLog); 42*795d594fSAndroid Build Coastguard Worker } 43*795d594fSAndroid Build Coastguard Worker } 44*795d594fSAndroid Build Coastguard Worker 45*795d594fSAndroid Build Coastguard Worker Object [] array = new Object[8000]; 46*795d594fSAndroid Build Coastguard Worker allocateFourStrings()47*795d594fSAndroid Build Coastguard Worker void allocateFourStrings() { 48*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < 4; i++) { 49*795d594fSAndroid Build Coastguard Worker String str = new String("Creating some garbage" + Math.random()); 50*795d594fSAndroid Build Coastguard Worker if (str.length() < 22) { 51*795d594fSAndroid Build Coastguard Worker System.out.println("bad length"); 52*795d594fSAndroid Build Coastguard Worker } 53*795d594fSAndroid Build Coastguard Worker } 54*795d594fSAndroid Build Coastguard Worker } 55*795d594fSAndroid Build Coastguard Worker stressArray(boolean doLog)56*795d594fSAndroid Build Coastguard Worker void stressArray(boolean doLog) { 57*795d594fSAndroid Build Coastguard Worker // We want many references in the array 58*795d594fSAndroid Build Coastguard Worker // We also want elements close to each other to have large 59*795d594fSAndroid Build Coastguard Worker // diff in address so lets skip every 2:nd address so it is null 60*795d594fSAndroid Build Coastguard Worker if (doLog) { 61*795d594fSAndroid Build Coastguard Worker System.out.println("Initializing..."); 62*795d594fSAndroid Build Coastguard Worker } 63*795d594fSAndroid Build Coastguard Worker for (int i = 0; i < array.length; i+=2) { 64*795d594fSAndroid Build Coastguard Worker array[i] = new String("Creating some garbage" + i); 65*795d594fSAndroid Build Coastguard Worker } 66*795d594fSAndroid Build Coastguard Worker 67*795d594fSAndroid Build Coastguard Worker if (doLog) { 68*795d594fSAndroid Build Coastguard Worker System.out.println("Starting the test"); 69*795d594fSAndroid Build Coastguard Worker } 70*795d594fSAndroid Build Coastguard Worker 71*795d594fSAndroid Build Coastguard Worker for (int j = 0; j < array.length; j++) { 72*795d594fSAndroid Build Coastguard Worker Object obj = array[array.length - 1]; 73*795d594fSAndroid Build Coastguard Worker System.arraycopy(array, 0, array, 1, array.length - 1); 74*795d594fSAndroid Build Coastguard Worker array[0] = obj; 75*795d594fSAndroid Build Coastguard Worker allocateFourStrings(); 76*795d594fSAndroid Build Coastguard Worker } 77*795d594fSAndroid Build Coastguard Worker 78*795d594fSAndroid Build Coastguard Worker for (int j = 0; j < array.length; j++) { 79*795d594fSAndroid Build Coastguard Worker Object obj = array[0]; 80*795d594fSAndroid Build Coastguard Worker System.arraycopy(array, 1, array, 0, array.length - 1); 81*795d594fSAndroid Build Coastguard Worker array[array.length - 1] = obj; 82*795d594fSAndroid Build Coastguard Worker allocateFourStrings(); 83*795d594fSAndroid Build Coastguard Worker } 84*795d594fSAndroid Build Coastguard Worker 85*795d594fSAndroid Build Coastguard Worker if (doLog) { 86*795d594fSAndroid Build Coastguard Worker System.out.println("Test OK"); 87*795d594fSAndroid Build Coastguard Worker } 88*795d594fSAndroid Build Coastguard Worker } 89*795d594fSAndroid Build Coastguard Worker } 90