1 /*
   2  * Copyright (c) 2013, 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  * @test
  26  * @key stress gc
  27  *
  28  * @summary converted from VM Testbase gc/memory/LargePagesTest.
  29  * VM Testbase keywords: [gc, stress, stressopt]
  30  *
  31  * @library /vmTestbase
  32  *          /test/lib
  33  * @run driver jdk.test.lib.FileInstaller . .
  34  * @run main/othervm gc.memory.LargePagesTest.LargePagesTest noThreads=5 duration=60
  35  */
  36 
  37 package gc.memory.LargePagesTest;
  38 
  39 import java.io.PrintStream;
  40 import java.util.Random;
  41 
  42 /*
  43  * Allocators purpose is to create pressure on the garbage collector
  44  * for a certain amount of time.
  45  * Note: this test moved from the "jr", the original name is func.vm.largepages.LargePagesTest
  46  */
  47 
  48 
  49 /**
  50  * @run main/othervm/timeout=150
  51  *      -XX:+UseLargePages
  52  *      -Xmx64m
  53  *      -Xms16m
  54  *      LargePagesTest
  55  *      noThreads=5 duration=60
  56  * @summary heap shrink/grow test
  57  */
  58 final public class LargePagesTest extends Thread {
  59 
  60     private static int cnt;
  61     private static final int SMALL_OBJECT_ALLOCATER = 1;
  62     private static final int LARGE_OBJECT_ALLOCATER = 2;
  63     private static final int ANY_OBJECT_ALLOCATER = 3;
  64     private static final int ANY_NO_MULTIARRAYS_ALLOCATER = 4;
  65     private int myType;
  66 
  67     /** Dummy thingies to update. */
  68     public LargePagesTest.Dummy[] d0;
  69     /** Dummy thingies to update. */
  70     public LargePagesTest.Dummy[] d1;
  71     /** Dummy thingies to update. */
  72     public LargePagesTest.Dummy[][] d2;
  73     /** Dummy thingies to update. */
  74     public LargePagesTest.Dummy[][] d3;
  75     /** Dummy thingies to update. */
  76     public LargePagesTest.Dummy[][][] d4;
  77     /** Dummy thingies to update. */
  78     public LargePagesTest.Dummy d5;
  79 
  80     /** Time to run execute(). */
  81     private long duration;
  82 
  83     /** Boolean of progress should be printed. */
  84     private boolean verbose;
  85 
  86     private static int noThreads = 5;
  87 
  88     /** Iterations. */
  89     public long iterations = 0L;
  90 
  91     /** Result. */
  92     public boolean result = false;
  93 
  94     private PrintStream out;
  95 
  96     /**
  97      * Creates DurAllocator.
  98      * @param name Parameter
  99      * @param duration Parameter
 100      * @param out Parameter
 101      * @param verbose Parameter
 102      */
 103     LargePagesTest(String name, long duration, PrintStream out, boolean verbose) {
 104 
 105         super(name);
 106         this.duration = duration;
 107         this.out = out;
 108         this.verbose = verbose;
 109     }
 110 
 111     /**
 112      * Print status.
 113      */
 114     void describe() {
 115         out.println("DurAllocator run: ");
 116         out.println("   test duration:     " + duration / 1000 + " seconds");
 117         out.println("   number of threads: " + noThreads + " threads");
 118     }
 119 
 120     /**
 121      * Allocates memory in a loop.
 122      */
 123     public void run() {
 124 
 125         long startTime = System.currentTimeMillis();
 126 
 127         while (System.currentTimeMillis() - startTime < duration) {
 128             try {
 129                 allocate();
 130             } catch (Throwable t) {
 131                 out.println(getName() + " FAILED: " + t.getClass().getName() + " in iteration " + iterations + ": " + t.getMessage());
 132                 return;
 133             }
 134             iterations++;
 135             if (verbose && iterations % 1000 == 0) {
 136                 out.print(".");
 137             }
 138             if (verbose && iterations % 60000 == 0) {
 139                 out.println(".");
 140             }
 141         }
 142         if (verbose) {
 143             out.println("");
 144         }
 145 
 146 
 147         long endTime = System.currentTimeMillis();
 148         long runTime = endTime - startTime;
 149         if (duration > runTime) {
 150             out.println(getName() + "  FAILED: Execution time < requested execution time.");
 151             out.println("                execution time is " + runTime);
 152             out.println("                requested time is " + duration);
 153         } else if (iterations <= 0) {
 154             out.println(getName() + "  FAILED: No executions finished");
 155         } else {
 156             result = true;
 157         }
 158     }
 159 
 160     private void allocate() {
 161 
 162         Random r = new Random();
 163         for (int j = 0; j < 1000; j++) {
 164             r = new Random();
 165             int i = 0;
 166 
 167             switch (myType) {
 168             case SMALL_OBJECT_ALLOCATER:
 169                 i = 5;
 170                 break;
 171             case LARGE_OBJECT_ALLOCATER:
 172                 i = 1;
 173                 break;
 174             case ANY_OBJECT_ALLOCATER:
 175                 i = r.nextInt(100);
 176                 break;
 177             case ANY_NO_MULTIARRAYS_ALLOCATER:
 178                 i = r.nextInt(100);
 179                 if ((i >= 2) && (i <= 4)) {
 180                     i = 5;
 181                 }
 182                 break;
 183             default:
 184                 break;
 185             }
 186 
 187             switch (i) {
 188             case 0:
 189                 d0 = new LargePagesTest.Dummy[10];
 190                 break;
 191             case 1:
 192                 d1 = new LargePagesTest.Dummy[1000];
 193                 break;
 194             case 2:
 195                 d2 = new LargePagesTest.Dummy[10][10];
 196                 break;
 197             case 3:
 198                 d3 = new LargePagesTest.Dummy[100][100];
 199                 break;
 200             case 4:
 201                 d4 = new LargePagesTest.Dummy[10][10][10];
 202                 break;
 203             default:
 204                 d5 = new LargePagesTest.Dummy();
 205                 break;
 206             }
 207         }
 208     }
 209 
 210     /**
 211      * A Dummy class.
 212      */
 213     private class Dummy {
 214         /**
 215          * Creates a dummy.
 216          */
 217         Dummy() {
 218             cnt++;
 219         }
 220     }
 221 
 222     /**
 223      * @param args Input arguments
 224      */
 225     public static void main(String[] args) {
 226 
 227         long duration = 30 * 60 * 1000;
 228         PrintStream out = System.out;
 229         boolean verbose = true;
 230 
 231         for (int i = 0; i < args.length; i++) {
 232             String noThreadsArgName = "noThreads=";
 233             String executionTimeArgName = "duration=";
 234             String verboseArgName = "verbose=";
 235             if (args[i].indexOf(noThreadsArgName) != -1) {
 236                 noThreads = Integer.parseInt(args[i].substring(noThreadsArgName.length(), args[i].length()));
 237             } else if (args[i].indexOf(executionTimeArgName) != -1) {
 238                 duration = 1000 * Long.parseLong(args[i].substring(executionTimeArgName.length(), args[i].length()));
 239             } else if (args[i].indexOf(verboseArgName) != -1) {
 240                 verbose = Boolean.parseBoolean(args[i].substring(verboseArgName.length(), args[i].length()));
 241             } else {
 242                 System.out.println("ERROR: Unknown argument string: " + args[i]);
 243                 System.exit(-1);
 244             }
 245         }
 246 
 247         // Please don't...
 248         if (noThreads <= 0) {
 249             noThreads = 1;
 250         }
 251 
 252         LargePagesTest[] runners = new LargePagesTest[noThreads];
 253 
 254         for (int i = 0; i < noThreads; i++) {
 255             runners[i] = new LargePagesTest("DurAllocator " + i, duration, out, verbose);
 256         }
 257 
 258         runners[0].describe();
 259 
 260         for (int i = 0; i < noThreads; i++) {
 261             runners[i].start();
 262         }
 263 
 264         for (int i = 0; i < noThreads; i++) {
 265             try {
 266                 runners[i].join(duration + 10 * 60 * 1000);
 267             } catch (InterruptedException e) {
 268                 out.println(runners[i].getName() + " FAILED: " + e.getClass().getName() + " " + e.getMessage());
 269                 System.exit(-1);
 270             }
 271         }
 272 
 273         for (int i = 0; i < noThreads; i++) {
 274             if (!runners[i].result) {
 275                 out.println(runners[i].getName() + " FAILED: status=" + runners[i].result);
 276                 System.exit(-1);
 277             }
 278         }
 279 
 280         if (verbose) {
 281             out.println();
 282         }
 283 
 284         out.print("DurAllocator PASSED with (");
 285         for (int i = 0; i < noThreads; i++) {
 286             out.print("" + runners[i].iterations + (i + 1 < noThreads ? "+" : ""));
 287         }
 288         out.println(") iterations.");
 289         // System.exit(90); // use to return 90 as indication of success
 290     }
 291 
 292 }