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 
 28 /* @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 */
 29 /* @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 */
 30 /* @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 */
 31 /* @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 */
 32 /* @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 */
 33 /* @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 */
 34 /* @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 */
 35 /* @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 */
 36 /* @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 */
 37 /* @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 */
 38 
 39 package gc.ArrayJuggle;
 40 
 41 import nsk.share.test.*;
 42 import nsk.share.gc.*;
 43 import nsk.share.gc.gp.*;
 44 
 45 /**
 46  * This test randomly replaces elements of an array with new objects
 47  * using given garbage producer and memory strategy.  This class was
 48  * renamed from Juggle01 to Juggle3 to better distinguice it from
 49  * Juggle1.
 50  */
 51 public class Juggle3 extends ThreadedGCTest implements GarbageProducerAware, MemoryStrategyAware {
 52         private GarbageProducer garbageProducer;
 53         private MemoryStrategy memoryStrategy;
 54         private Object[] array;
 55         private Object[] indexLocks;
 56         long objectSize;
 57 
 58         private class Juggler implements Runnable {
 59                 public void run() {
 60                         int index = LocalRandom.nextInt(array.length);
 61                         // Synchronizing to prevent multiple object creation for the same index at the same time.
 62                         synchronized (indexLocks[index]) {
 63                                 array[index] = null;
 64                                 array[index] = garbageProducer.create(objectSize);
 65                         }
 66                 }
 67         }
 68 
 69         protected Runnable createRunnable(int i) {
 70                 return new Juggler();
 71         }
 72 
 73         public void run() {
 74                 log.debug("Garbage producer: " + garbageProducer);
 75                 log.debug("Memory strategy: " + memoryStrategy);
 76                 long memory = runParams.getTestMemory();
 77                 int objectCount = memoryStrategy.getCount(memory);
 78                 objectSize = memoryStrategy.getSize(memory);
 79                 log.debug("Object count: " + objectCount);
 80                 log.debug("Object size: " + objectSize);
 81                 array = new Object[objectCount - 1];
 82                 indexLocks = new Object[objectCount - 1];
 83                 for (int i = 0; i < indexLocks.length; i++) {
 84                     indexLocks[i] = new Object();
 85                 }
 86                 super.run();
 87         }
 88 
 89         public void setGarbageProducer(GarbageProducer garbageProducer) {
 90                 this.garbageProducer = garbageProducer;
 91         }
 92 
 93         public void setMemoryStrategy(MemoryStrategy memoryStrategy) {
 94                 this.memoryStrategy = memoryStrategy;
 95         }
 96 
 97         public static void main(String[] args) {
 98                 GC.runTest(new Juggle3(), args);
 99         }
100 }