1 /*
   2  * Copyright (c) 2002, 2018, 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
  28  *
  29  * @summary converted from VM Testbase gc/memory/Array/ArrayJuggle/Juggle2.
  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.Juggle2.Juggle2
  36  */
  37 
  38 package gc.memory.Array.ArrayJuggle.Juggle2;
  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 Juggle2 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 = 30;
  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                         //for (int i = 0; i < thisChainLength; ++i)
  82                         //      mainArray[index] = new LinkedMemoryObject(cellSize, mainArray[index]);
  83                         //Memory.makeLinearList(maxLinkLength, objectSize);
  84                         mainArray[index] = Memory.makeRandomLinearList(maxLinkLength, objectSize);
  85                 }
  86 
  87                 public String toString() {
  88                         return "Thread-B";
  89                 }
  90         }
  91 
  92         private class CopyingThread implements Runnable {
  93                 private LinkedMemoryObject localArray[];
  94                 private int currentIndex = 0;
  95 
  96                 public CopyingThread() {
  97                         localArray = new LinkedMemoryObject[maxCopySize];
  98                         for (int i = 0; i < maxCopySize; ++i)
  99                                 localArray[i] = new LinkedMemoryObject(0);
 100                 }
 101 
 102                 public void run() {
 103                         int toCopy = LocalRandom.nextInt(maxCopySize);
 104                         int mainIndex = LocalRandom.nextInt(arraySize);
 105                         for (int i = 0; i < toCopy; i++) {
 106                                 localArray[currentIndex].setNext(mainArray[mainIndex]);
 107                                 currentIndex = (currentIndex + 1) % maxCopySize;
 108                                 mainIndex = (mainIndex + 1) % arraySize;
 109                         }
 110                 }
 111 
 112                 public String toString() {
 113                         return "Thread-C";
 114                 }
 115         }
 116 
 117         protected Runnable createRunnable(int i) {
 118                 switch (i % 3) {
 119                 case 0:
 120                         return new MainArrayWalker();
 121                 case 1:
 122                         return new LinkMaker();
 123                 case 2:
 124                 default:
 125                         return new CopyingThread();
 126                 }
 127         }
 128 
 129         public void run() {
 130                 // arraySize * (objectSize + referenceSize) + threadsCount * (referenceSize arraySize/10 * (referenceSize + objectSize)) = memory
 131                 long referenceSize = Memory.getReferenceSize();
 132                 long objectExtraSize = Memory.getObjectExtraSize();
 133                 arraySize = Memory.getArrayLength(
 134                         runParams.getTestMemory(),
 135                         Memory.getListSize(maxLinkLength, objectSize)
 136                 );
 137                 maxCopySize = arraySize / 10 - 1;
 138                 arraySize = arraySize * 9 / 10 - 1;
 139                 System.out.println("Array size: " + arraySize);
 140                 mainArray  = new LinkedMemoryObject[arraySize];
 141                 Memory.fillArrayRandom(mainArray, arraySize, objectSize);
 142                 super.run();
 143         }
 144 
 145         public static void main(String args[]) {
 146                 GC.runTest(new Juggle2(), args);
 147         }
 148 }