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 /*  stress testing
  25  Redthreads keep removing new nodes from a binary sort tree(
  26  all nodes of its left subtree is less than itself, all nodes
  27  of its right subtree is large than itself).
  28  Bluethreads keep adding nodes into the binary sort tree.
  29  YellowThreads search the binary sort tree.
  30  The nodes removed from the tree will become garbages immediately
  31  Create 10 Redthreads and 10 Bluethreads to manipulate the
  32  the same binary tree involving excessive memory allocation
  33  to test if memory management module and gc() crash.
  34  */
  35 
  36 
  37 /*
  38  * @test
  39  * @key gc
  40  *
  41  * @summary converted from VM Testbase gc/gctests/gctest03.
  42  * VM Testbase keywords: [gc]
  43  *
  44  * @library /vmTestbase
  45  *          /test/lib
  46  * @run driver jdk.test.lib.FileInstaller . .
  47  * @compile Tree.java appthread.java
  48  * @run main/othervm gc.gctests.gctest03.gctest03 10000
  49  */
  50 
  51 package gc.gctests.gctest03;
  52 
  53 import nsk.share.test.*;
  54 import nsk.share.gc.*;
  55 import nsk.share.TestFailure;
  56 import nsk.share.TestBug;
  57 
  58 //import Tree;
  59 //import Redthread;
  60 //import Bluethread;
  61 //import LocalRandom;
  62 import java.util.Random;
  63 
  64 public class gctest03 extends TestBase {
  65         private String[] args;
  66 
  67         public gctest03(String[] args) {
  68                 this.args = args;
  69         }
  70 
  71         public void run() {
  72                 int i = 1;
  73                 int  dataNodeLimit = 100000;
  74 
  75                 if (args.length > 0) {
  76                         try {
  77                                 dataNodeLimit = new Integer(args[0]).intValue();
  78                         } catch (NumberFormatException e) {
  79                                 throw new TestBug("Bad input to gctest03. Expected integer, " +
  80                                                 " got: ->" + args[0] + "<-", e);
  81                         }
  82                 }
  83 
  84                 for (int j = 0; j < 10; j++) {
  85                         DataNode.setDataNodeLimit(dataNodeLimit);
  86                         DataNode.clearDataNodeCount();
  87 
  88                         Tree  tr = new Tree();
  89                         for (i =2; i < 100; i++) {
  90                                 try {
  91                                         DataNode d = new DataNode(i);
  92                                         TreeNode t = new TreeNode(d);
  93                                         tr.insert(t);
  94                                 } catch (DataNodeException e) {
  95                                         throw new TestFailure("DataNodeException caught in gctest03.main()", e);
  96                                 }
  97                         }
  98 
  99                         int sz = 10;
 100 
 101                         //create 10 threads adding data into binary tree.
 102                         // each thread only adds the multiple of its key
 103                         //(1, 2, 3, 4, 5, 6, 7, 8, 9 , 10). No duplication
 104 
 105                         Redthread rth[] = new Redthread[sz];
 106 
 107                         for(i=0; i < sz; i++) {
 108                                 rth[i] = new Redthread(tr, i+1);
 109                                 rth[i].setName("Redthread" + i);
 110                                 rth[i].start();
 111                         }
 112 
 113                         //create 10 threads removing data from the tree.
 114 
 115                         Bluethread bth[] = new Bluethread[sz];
 116 
 117                         for(i=0; i < sz; i++) {
 118                                 bth[i] = new Bluethread(tr, i+1);
 119                                 bth[i].setName("Bluethread" + i);
 120                                 bth[i].start();
 121                         }
 122 
 123 
 124                         //create 10 threads inquiring data from the tree
 125 
 126                         Yellowthread yth[] = new Yellowthread[sz];
 127                         for(i=0; i < sz; i++) {
 128                                 yth[i] = new Yellowthread(tr, i+1);
 129                                 yth[i].setName("Yellowthread" + i);
 130                                 yth[i].start();
 131                         }
 132 
 133                         for (i = 0; i < sz; i++) {
 134                                 try {
 135                                         rth[i].join();
 136                                         bth[i].join();
 137                                         yth[i].join();
 138                                 } catch (InterruptedException e) {
 139                                         System.err.println("Error joining with threads in gctest03.main()");
 140                                         System.err.println("Loop count: " + i);
 141                                 }
 142                         }
 143                 }
 144 
 145         }
 146 
 147         public static void main(String args[]) {
 148                 GC.runTest(new gctest03(args), args);
 149         }
 150 }