1 /*
  2  * Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]
 26  */
 27 /* @test id=BooleanObjArr_medium   @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp BooleanObjArr        -ms medium */
 28 /* @test id=ByteObjArr_high        @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp ByteObjArr           -ms high */
 29 /* @test id=IntegerObjArr_low      @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp IntegerObjArr        -ms low */
 30 /* @test id=h_IntegerObjArr_low    @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp  hashed(IntegerObjArr) -ms low */
 31 
 32 /* @test id=booleanArr_medium  @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp booleanArr        -ms medium */
 33 /* @test id=floatArr_high      @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp floatArr          -ms high */
 34 /* @test id=objectArr_low      @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp objectArr         -ms low */
 35 /* @test id=objectArr_medium   @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp objectArr         -ms medium */
 36 /* @test id=objectArr_high     @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp objectArr         -ms high */
 37 /* @test id=h_doubleArr_low    @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp hashed(doubleArr) -ms low */
 38 /* @test id=h_doubleArr_high   @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp hashed(doubleArr) -ms high */
 39 /* @test id=h_objectArr_low    @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp hashed(objectArr) -ms low */
 40 /* @test id=h_objectArr_medium @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp hashed(objectArr) -ms medium */
 41 /* @test id=h_objectArr_high   @key stress randomness @library /vmTestbase /test/lib @run main/othervm -XX:+HeapDumpOnOutOfMemoryError -Xlog:gc=debug:gc.log gc.ArrayJuggle.Juggle3 -gp hashed(objectArr) -ms high */
 42 
 43 package gc.ArrayJuggle;
 44 
 45 import nsk.share.test.*;
 46 import nsk.share.gc.*;
 47 import nsk.share.gc.gp.*;
 48 
 49 /**
 50  * This test randomly replaces elements of an array with new objects
 51  * using given garbage producer and memory strategy.  This class was
 52  * renamed from Juggle01 to Juggle3 to better distinguice it from
 53  * Juggle1.
 54  */
 55 public class Juggle3 extends ThreadedGCTest implements GarbageProducerAware, MemoryStrategyAware {
 56         private GarbageProducer garbageProducer;
 57         private MemoryStrategy memoryStrategy;
 58         private Object[] array;
 59         private Object[] indexLocks;
 60         long objectSize;
 61 
 62         private class Juggler implements Runnable {
 63                 public void run() {
 64                         int index = LocalRandom.nextInt(array.length);
 65                         // Synchronizing to prevent multiple object creation for the same index at the same time.
 66                         synchronized (indexLocks[index]) {
 67                                 array[index] = null;
 68                                 array[index] = garbageProducer.create(objectSize);
 69                         }
 70                 }
 71         }
 72 
 73         protected Runnable createRunnable(int i) {
 74                 return new Juggler();
 75         }
 76 
 77         public void run() {
 78                 log.debug("Garbage producer: " + garbageProducer);
 79                 log.debug("Memory strategy: " + memoryStrategy);
 80                 long memory = runParams.getTestMemory();
 81                 int objectCount = memoryStrategy.getCount(memory);
 82                 objectSize = memoryStrategy.getSize(memory);
 83                 log.debug("Object count: " + objectCount);
 84                 log.debug("Object size: " + objectSize);
 85                 array = new Object[objectCount - 1];
 86                 indexLocks = new Object[objectCount - 1];
 87                 for (int i = 0; i < indexLocks.length; i++) {
 88                     indexLocks[i] = new Object();
 89                 }
 90                 super.run();
 91         }
 92 
 93         public void setGarbageProducer(GarbageProducer garbageProducer) {
 94                 this.garbageProducer = garbageProducer;
 95         }
 96 
 97         public void setMemoryStrategy(MemoryStrategy memoryStrategy) {
 98                 this.memoryStrategy = memoryStrategy;
 99         }
100 
101         public static void main(String[] args) {
102                 GC.runTest(new Juggle3(), args);
103         }
104 }