1 /*
   2  * Copyright (c) 2003, 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/gctests/Steal/steal002.
  29  * VM Testbase keywords: [gc, stress, stressopt, nonconcurrent]
  30  * VM Testbase readme:
  31  * DESCRIPTION
  32  *     The test checks that Garbage Collector correctly uses stealing technique:
  33  *     no unexpected exceptions and errors are thrown; the JVM is not crashed.
  34  *     Actually, the test is intended for Parallel Collector.
  35  *     The test starts just one thread, then creates a small NonbranyTree and a
  36  *     huge one. Both trees are to fill about 80% of the memory. Then the test
  37  *     drops references to both trees and invoke Algorithms.eatMemory(int) to
  38  *     provoke GC to clean the memory. the GC should correctly remove both
  39  *     objects. If the GC is Parallel, there are more than one GC threads, so one
  40  *     will try to "steal" some job from others.
  41  *
  42  * @library /vmTestbase
  43  *          /test/lib
  44  * @run driver jdk.test.lib.FileInstaller . .
  45  * @run main/othervm -XX:-UseGCOverheadLimit gc.gctests.Steal.steal002.steal002
  46  */
  47 
  48 package gc.gctests.Steal.steal002;
  49 
  50 import nsk.share.gc.*;
  51 import nsk.share.test.ExecutionController;
  52 import nsk.share.test.Stresser;
  53 
  54 public class steal002 extends GCTestBase {
  55 
  56     ExecutionController stresser;
  57     // Number of nodes for the small tree
  58     final static int SMALL_NODES = 10;
  59     // Size (in bytes) for a node of the small tree
  60     final static int SMALL_NODE_SIZE = 1;
  61     // Number of nodes for the huge tree
  62     final static int HUGE_NODES = 500;
  63     // Part of the heap to fill with both trees
  64     final static double PART_OF_HEAP = 0.8;
  65     final int hugeNodeSize;
  66     public static NonbranchyTree smallTree;
  67     public static NonbranchyTree hugeTree;
  68 
  69     @Override
  70     public void run() {
  71         if (stresser == null) {
  72             stresser = new Stresser(runParams.getStressOptions());
  73             stresser.start(runParams.getIterations());
  74         }
  75         while (stresser.continueExecution()) {
  76             // Create a small tree and a huge one. Then drop references
  77             // to both of them.
  78             smallTree = new NonbranchyTree(SMALL_NODES, 0.3f, SMALL_NODE_SIZE);
  79             hugeTree = new NonbranchyTree(HUGE_NODES, 0.3f, hugeNodeSize);
  80 
  81             // Drop references to both trees and provoke GC to clean
  82             // the memory
  83             hugeTree = null;
  84             smallTree = null;
  85 
  86             // Provoke GC to clean the memory
  87             Algorithms.eatMemory(stresser);
  88         }
  89     }
  90 
  91     public steal002() {
  92         hugeNodeSize = Math.max(1, (int) (PART_OF_HEAP * Runtime.getRuntime().maxMemory() / HUGE_NODES
  93                 - NonbranchyTree.MIN_NODE_SIZE));
  94     }
  95 
  96     public static void main(String args[]) {
  97         GC.runTest(new steal002(), args);
  98     }
  99 }