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/gctests/ThreadGC.
  30  * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]
  31  * VM Testbase readme:
  32  * This tests attempts to stress the garbage collector my making
  33  * synchronous calls to the garbage collector after producing garbage.
  34  * The garbage collector is invoked in a separate thread.
  35  * The test runs for one minute (see nsk.share.runner.ThreadsRunner and
  36  * nsk.share.runner.RunParams. It passes if no exceptions are generated.
  37  *
  38  * @library /vmTestbase
  39  *          /test/lib
  40  * @run driver jdk.test.lib.FileInstaller . .
  41  * @run main/othervm gc.gctests.ThreadGC.ThreadGC -gp random(arrays) -ms low
  42  */
  43 
  44 package gc.gctests.ThreadGC;
  45 
  46 import nsk.share.gc.*;
  47 import nsk.share.gc.gp.*;
  48 import java.util.*;
  49 
  50 public class ThreadGC extends ThreadedGCTest implements GarbageProducerAware, MemoryStrategyAware {
  51         private GarbageProducer garbageProducer;
  52         private MemoryStrategy memoryStrategy;
  53         private Reclaimer reclaimer;
  54         private int count;
  55         private long size;
  56 
  57         private class Devourer implements Runnable {
  58                 private Object[] arr = null;
  59                 private int index;
  60 
  61                 public void run() {
  62                         if (arr == null || index >= count) {
  63                                 arr = null;
  64                                 arr = new Object[count];
  65                                 index = 0;
  66                                 synchronized (reclaimer) {
  67                                         reclaimer.notify();
  68                                 }
  69                         }
  70                         arr[index] = garbageProducer.create(size);
  71                         ++index;
  72                 }
  73         }
  74 
  75         private class Reclaimer implements Runnable {
  76                 private long waitTime = 1000;
  77 
  78                 public void run() {
  79                         try {
  80                                 synchronized (this) {
  81                                         this.wait(waitTime);
  82                                 }
  83                         } catch (InterruptedException e) {
  84                         }
  85                         System.gc();
  86                 }
  87         }
  88 
  89         protected Runnable createRunnable(int i) {
  90                 if (i == 0)
  91                         return new Devourer();
  92                 else if (i == 1) {
  93                         reclaimer = new Reclaimer();
  94                         return reclaimer;
  95                 } else
  96                         return null;
  97         }
  98 
  99         public void run() {
 100                 size = GarbageUtils.getArraySize(runParams.getTestMemory(), memoryStrategy);
 101                 count = GarbageUtils.getArrayCount(runParams.getTestMemory(), memoryStrategy);
 102                 runParams.setIterations(count);
 103                 super.run();
 104         }
 105 
 106         public static void main(String[] args) {
 107                 GC.runTest(new ThreadGC(), args);
 108         }
 109 
 110         public void setGarbageProducer(GarbageProducer garbageProducer) {
 111                 this.garbageProducer = garbageProducer;
 112         }
 113 
 114         public void setMemoryStrategy(MemoryStrategy memoryStrategy) {
 115                 this.memoryStrategy = memoryStrategy;
 116         }
 117 
 118 }