import java.util.*; public class gctest { public static final int numstacks = 10; public static int iters = 1000; public static void testRecursive(int i) { if (i > iters) return; Integer stackobj = new Integer(Ti.thisProc()*10000000 + i); testRecursive(i+1); if (stackobj.intValue() != Ti.thisProc()*10000000 + i) { System.out.println("recursive stack object error detected on thread " + Ti.thisProc() + "!!!"); System.exit(1); } } public static void main(String[]args) { Stack [] s = new Stack[numstacks]; Integer stackobj = new Integer(Ti.thisProc()); if (args.length > 0) try { iters = Integer.parseInt(args[0]); } catch (Throwable exn) {} if (Ti.thisProc() == 0) System.out.println("running " + iters + " iterations of gc test"); Ti.barrier(); for (int a = 0; a < numstacks; a++) { s[a] = new Stack(); doWork(); for (int i = 0; i < iters; i++) { s[a].push(new Integer((Ti.thisProc())*1000000 + i)); } System.gc(); } doWork(); testRecursive(0); Ti.barrier(); if (Ti.thisProc() == 0) System.out.println("verifying..."); if (stackobj.intValue() != Ti.thisProc()) { System.out.println("stack object error detected on thread " + Ti.thisProc() + "!!!"); System.exit(1); } for (int a = 0; a < numstacks; a++) { for (int i = iters-1; i >= 0; i--) { Integer io = (Integer)s[a].pop(); if (io.intValue() != Ti.thisProc()*1000000 + i) { System.out.println("heap object error detected on thread " + Ti.thisProc() + "!!!"); System.exit(1); } } } Ti.barrier(); if (Ti.thisProc() == 0) System.out.println("done."); } public static double junk; public static Random rand = new Random(); public static void doWork() { double x = 1.00001; int [] junkarr = new int[16535]; try { Thread.sleep((int)(Ti.thisProc()*10)); } catch (InterruptedException exn) { throwInternalError("thread was interrupted during a sleep in doWork"); } for (int i = 0; i < 100000; i++) { x = x * 0.99999 / 0.9999; } try { Thread.sleep(rand.nextDouble() > 0.95 ? 1000 : 0); // for platforms w/o usleep } catch (InterruptedException exn) { throwInternalError("thread was interrupted during a sleep in doWork"); } junk = x; Thread.yield(); } public static void throwInternalError(String s) { throw new InternalError(s); } }