1 /*
   2  * Copyright (c) 2003, 2015, 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  * @bug 4882798
  27  * @summary multi-thread test to exercise sync and contention for removes to transformer registry
  28  * @author Gabriel Adauto, Wily Technology
  29  *
  30  * @modules java.instrument
  31  * @run build TransformerManagementThreadRemoveTests
  32  * @run shell MakeJAR.sh redefineAgent
  33  * @run main/othervm -javaagent:redefineAgent.jar TransformerManagementThreadRemoveTests TransformerManagementThreadRemoveTests
  34  */
  35 import java.util.*;
  36 
  37 public class TransformerManagementThreadRemoveTests
  38     extends TransformerManagementThreadAddTests
  39 {
  40 
  41     /**
  42      * Constructor for TransformerManagementThreadRemoveTests.
  43      * @param name
  44      */
  45     public TransformerManagementThreadRemoveTests(String name)
  46     {
  47         super(name);
  48     }
  49 
  50     public static void
  51     main (String[] args)
  52         throws Throwable {
  53         ATestCaseScaffold   test = new TransformerManagementThreadRemoveTests(args[0]);
  54         test.runTest();
  55     }
  56 
  57     protected final void
  58     doRunTest()
  59         throws Throwable {
  60         testMultiThreadAddsAndRemoves();
  61     }
  62 
  63     public void
  64     testMultiThreadAddsAndRemoves()
  65     {
  66         int size = TOTAL_THREADS + REMOVE_THREADS;
  67         ArrayList threadList = new ArrayList(size);
  68         for (int i = MIN_TRANS; i <= MAX_TRANS; i++)
  69         {
  70             int index = i - MIN_TRANS;
  71             threadList.add(new TransformerThread("Trans"+prettyNum(index,2), i));
  72         }
  73 
  74         int factor = (int)Math.floor(TOTAL_THREADS / REMOVE_THREADS);
  75         for (int i = 0; i < REMOVE_THREADS; i++)
  76         {
  77             threadList.add(factor * i, new RemoveThread("Remove"+i));
  78         }
  79 
  80         Thread[] threads = (Thread[])threadList.toArray(new Thread[size]);
  81         setExecThread(new ExecuteTransformersThread());
  82         getExecThread().start();
  83         for (int i = threads.length - 1; i >= 0; i--)
  84         {
  85             threads[i].start();
  86         }
  87 
  88         while (!testCompleted())
  89         {
  90             // Effective Java - Item 51: Don't depend on the thread scheduler
  91             // Use sleep() instead of yield().
  92             try {
  93                 Thread.sleep(500);
  94             } catch (InterruptedException ie) {
  95             }
  96         }
  97         assertTrue(finalCheck());
  98 
  99         //printTransformers();
 100     }
 101 
 102     /**
 103      * Method removeTransformer.
 104      */
 105     private void
 106     removeTransformer(Thread t)
 107     {
 108         ThreadTransformer tt = null;
 109 
 110         synchronized (fAddedTransformers)
 111         {
 112             int size = fAddedTransformers.size();
 113             if (size > 0)
 114             {
 115                 int choose = (int)Math.floor(Math.random() * size);
 116                 tt = (ThreadTransformer)fAddedTransformers.remove(choose);
 117             }
 118             //System.out.println("removed("+tt+") size("+size+") chose("+choose+") by("+t+")");
 119         }
 120 
 121         if (tt != null)
 122         {
 123             getInstrumentation().removeTransformer(tt);
 124         }
 125     }
 126 
 127     private class
 128     RemoveThread
 129         extends Thread
 130     {
 131 
 132         RemoveThread(String name)
 133         {
 134             super(name);
 135         }
 136 
 137         public void
 138         run()
 139         {
 140             while (!threadsDone())
 141             {
 142                 removeTransformer(RemoveThread.this);
 143             }
 144         }
 145     }
 146 
 147 }