1 /*
   2  * Copyright (c) 2002, 2020, 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 /*
  26  * @test
  27  * @key stress gc randomness
  28  *
  29  * @summary converted from VM Testbase gc/memory/Array/ArrayJuggle/Juggle1.
  30  * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]
  31  *
  32  * @library /vmTestbase
  33  *          /test/lib
  34  * @run driver jdk.test.lib.FileInstaller . .
  35  * @run main/othervm -Xlog:gc=debug:gc.log gc.memory.Array.ArrayJuggle.Juggle1.Juggle1
  36  */
  37 
  38 package gc.memory.Array.ArrayJuggle.Juggle1;
  39 
  40 import nsk.share.test.*;
  41 import nsk.share.gc.*;
  42 
  43 /**
  44  * Test that tries to confuse the GC.
  45  *
  46  * This program initializes a main array and launches some threads
  47  * which modify and copy portions of the array to try to confuse
  48  * the GC.
  49  */
  50 public class Juggle1 extends ThreadedGCTest {
  51         private int arraySize = 1000;
  52         private int objectSize = 1000;
  53         private int maxLinkLength = 100;
  54         private int maxCopySize = arraySize / 10;
  55         private int threadsCount;
  56         private LinkedMemoryObject mainArray[];
  57 
  58         private class MainArrayWalker implements Runnable {
  59                 public void run() {
  60                         int index = LocalRandom.nextInt(arraySize);
  61                         int cellSize = LocalRandom.nextInt(objectSize);
  62                         mainArray[index] = new LinkedMemoryObject(cellSize);
  63                         //mainArray[index] = Memory.makeLinearList(maxLinkLength, objectSize);
  64                 }
  65 
  66                 public String toString() {
  67                         return "Thread-A";
  68                 }
  69         }
  70 
  71         private class LinkMaker implements Runnable {
  72                 private int n;
  73 
  74                 public void run() {
  75                         int index = LocalRandom.nextInt(arraySize);
  76                         // Sometimes clear the reference so the lists do not become too large
  77                         if (++n == maxLinkLength) {
  78                                 mainArray[index] = null;
  79                                 n = 0;
  80                         }
  81                         mainArray[index] = Memory.makeRandomLinearList(maxLinkLength, objectSize);
  82                 }
  83 
  84                 public String toString() {
  85                         return "Thread-B";
  86                 }
  87         }
  88 
  89         private class CopyingThread implements Runnable {
  90                 private LinkedMemoryObject localArray[];
  91                 private int currentIndex = 0;
  92 
  93                 public CopyingThread() {
  94                         localArray = new LinkedMemoryObject[maxCopySize];
  95                 }
  96 
  97                 public void run() {
  98                         int toCopy = LocalRandom.nextInt(maxCopySize);
  99                         int mainIndex = LocalRandom.nextInt(arraySize);
 100                         for (int i = 0; i < toCopy; i++) {
 101                                 localArray[currentIndex] = mainArray[mainIndex];
 102                                 currentIndex = (currentIndex + 1) % maxCopySize;
 103                                 mainIndex = (mainIndex + 1) % arraySize;
 104                         }
 105                 }
 106 
 107                 public String toString() {
 108                         return "Thread-C";
 109                 }
 110         }
 111 
 112         protected Runnable createRunnable(int i) {
 113                 switch (i % 3) {
 114                 case 0:
 115                         return new MainArrayWalker();
 116                 case 1:
 117                         return new LinkMaker();
 118                 case 2:
 119                 default:
 120                         return new CopyingThread();
 121                 }
 122         }
 123 
 124         public void run() {
 125                 // arraySize * (objectSize + referenceSize) + threadsCount * (referenceSize arraySize/10 * (referenceSize + objectSize)) = memory
 126                 long referenceSize = Memory.getReferenceSize();
 127                 long objectExtraSize = Memory.getObjectExtraSize();
 128                 threadsCount = runParams.getNumberOfThreads();
 129                 arraySize = Memory.getArrayLength(
 130                         runParams.getTestMemory(),
 131                         Memory.getListSize(maxLinkLength, objectSize)
 132                 );
 133                 maxCopySize = arraySize / 10 - 1;
 134                 arraySize = arraySize * 9 / 10 - 1;
 135                 System.out.println("Array size: " + arraySize);
 136                 mainArray  = new LinkedMemoryObject[arraySize];
 137                 Memory.fillArrayRandom(mainArray, arraySize, objectSize);
 138                 super.run();
 139         }
 140 
 141         public static void main(String args[]) {
 142                 GC.runTest(new Juggle1(), args);
 143         }
 144 }