1 /*
   2  * Copyright (c) 2002, 2020, 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 randomness
  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 
  62 public class gctest03 extends TestBase {
  63         private String[] args;
  64 
  65         public gctest03(String[] args) {
  66                 this.args = args;
  67         }
  68 
  69         public void run() {
  70                 int i = 1;
  71                 int  dataNodeLimit = 100000;
  72 
  73                 if (args.length > 0) {
  74                         try {
  75                                 dataNodeLimit = new Integer(args[0]).intValue();
  76                         } catch (NumberFormatException e) {
  77                                 throw new TestBug("Bad input to gctest03. Expected integer, " +
  78                                                 " got: ->" + args[0] + "<-", e);
  79                         }
  80                 }
  81 
  82                 for (int j = 0; j < 10; j++) {
  83                         DataNode.setDataNodeLimit(dataNodeLimit);
  84                         DataNode.clearDataNodeCount();
  85 
  86                         Tree  tr = new Tree();
  87                         for (i =2; i < 100; i++) {
  88                                 try {
  89                                         DataNode d = new DataNode(i);
  90                                         TreeNode t = new TreeNode(d);
  91                                         tr.insert(t);
  92                                 } catch (DataNodeException e) {
  93                                         throw new TestFailure("DataNodeException caught in gctest03.main()", e);
  94                                 }
  95                         }
  96 
  97                         int sz = 10;
  98 
  99                         //create 10 threads adding data into binary tree.
 100                         // each thread only adds the multiple of its key
 101                         //(1, 2, 3, 4, 5, 6, 7, 8, 9 , 10). No duplication
 102 
 103                         Redthread rth[] = new Redthread[sz];
 104 
 105                         for(i=0; i < sz; i++) {
 106                                 rth[i] = new Redthread(tr, i+1);
 107                                 rth[i].setName("Redthread" + i);
 108                                 rth[i].start();
 109                         }
 110 
 111                         //create 10 threads removing data from the tree.
 112 
 113                         Bluethread bth[] = new Bluethread[sz];
 114 
 115                         for(i=0; i < sz; i++) {
 116                                 bth[i] = new Bluethread(tr, i+1);
 117                                 bth[i].setName("Bluethread" + i);
 118                                 bth[i].start();
 119                         }
 120 
 121 
 122                         //create 10 threads inquiring data from the tree
 123 
 124                         Yellowthread yth[] = new Yellowthread[sz];
 125                         for(i=0; i < sz; i++) {
 126                                 yth[i] = new Yellowthread(tr, i+1);
 127                                 yth[i].setName("Yellowthread" + i);
 128                                 yth[i].start();
 129                         }
 130 
 131                         for (i = 0; i < sz; i++) {
 132                                 try {
 133                                         rth[i].join();
 134                                         bth[i].join();
 135                                         yth[i].join();
 136                                 } catch (InterruptedException e) {
 137                                         System.err.println("Error joining with threads in gctest03.main()");
 138                                         System.err.println("Loop count: " + i);
 139                                 }
 140                         }
 141                 }
 142 
 143         }
 144 
 145         public static void main(String args[]) {
 146                 GC.runTest(new gctest03(args), args);
 147         }
 148 }