1 /*
   2  * Copyright (c) 2007, 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/vector/SimpleGC.
  29  * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent, monitoring]
  30  *
  31  * @library /vmTestbase
  32  *          /test/lib
  33  * @run driver jdk.test.lib.FileInstaller . .
  34  * @run main/othervm gc.vector.SimpleGC.SimpleGC -ms high
  35  */
  36 
  37 package gc.vector.SimpleGC;
  38 
  39 import nsk.share.gc.GC;
  40 import nsk.share.gc.GCTestBase;
  41 import nsk.share.gc.gp.GarbageProducer;
  42 import nsk.share.gc.gp.GarbageProducerAware;
  43 import nsk.share.gc.gp.GarbageUtils;
  44 import nsk.share.gc.gp.MemoryStrategy;
  45 import nsk.share.gc.gp.MemoryStrategyAware;
  46 import nsk.share.runner.RunParams;
  47 import nsk.share.test.Stresser;
  48 
  49 /**
  50  * Test that fills out a certain amount of memory with objects of a given type.
  51  * The test accepts  MemoryStrategy, type of objects and stress options
  52  * as command line args.
  53  */
  54 public class SimpleGC extends GCTestBase implements GarbageProducerAware, MemoryStrategyAware {
  55 
  56     private GarbageProducer garbageProducer;
  57     private MemoryStrategy memoryStrategy;
  58 
  59     /**
  60      * Garbage link.
  61      * The field is static to prevent possible compiler optimizations.
  62      */
  63     public static Object[] array;
  64 
  65     @Override
  66     public void run() {
  67 
  68         long memoryAmount = runParams.getTestMemory();
  69         long size = GarbageUtils.getArraySize(memoryAmount, memoryStrategy);
  70         int length = GarbageUtils.getArrayCount(memoryAmount, memoryStrategy);
  71         Object[] garbage = new Object[length];
  72         array = garbage;
  73         log.info("Memory to fill out: " + memoryAmount);
  74         log.info("Array lenght: " + garbage.length);
  75         log.info("Object size: " + size);
  76         log.info("Garbage producer: " + garbageProducer.getClass().getCanonicalName());
  77         log.info("Memory Strategy: " + memoryStrategy);
  78         Stresser stresser = new Stresser(runParams.getStressOptions());
  79         stresser.start(10 * garbage.length);
  80 
  81         int iteration = 0;
  82         try {
  83             while (stresser.iteration()) {
  84                 for (int i = 0; i < garbage.length && stresser.continueExecution(); ++i) {
  85                     garbage[i] = garbageProducer.create(size);
  86                 }
  87                 for (int i = 0; i < garbage.length; ++i) {
  88                     garbage[i] = null;
  89                 }
  90                 iteration++;
  91                 if (iteration % 1000 == 0) {
  92                     log.info("  iteration # " + iteration);
  93                 }
  94             }
  95         } catch (OutOfMemoryError oom) {
  96             // normally that should never be thrown
  97             log.info("OutOfMemory after " + iteration + " iterations");
  98             throw oom;
  99         } finally {
 100             stresser.finish();
 101         }
 102         log.info("Success. Total iterations: " + iteration);
 103     }
 104 
 105     @Override
 106     public final void setGarbageProducer(GarbageProducer garbageProducer) {
 107         this.garbageProducer = garbageProducer;
 108     }
 109 
 110     @Override
 111     public final void setMemoryStrategy(MemoryStrategy memoryStrategy) {
 112         this.memoryStrategy = memoryStrategy;
 113     }
 114 
 115     public static void main(String[] args) {
 116         RunParams.getInstance().setRunMemDiagThread(true);
 117         GC.runTest(new SimpleGC(), args);
 118     }
 119 }