jdk/test/java/lang/instrument/TransformerManagementThreadRemoveTests.java
author dcubed
Mon, 24 Mar 2008 15:14:31 -0700
changeset 273 3d51c181a2e5
parent 271 696ff142e6e6
child 715 f16baef3a20e
permissions -rw-r--r--
6274276: 3/2 java.lang.instrument JAR manifest processing does not remove spaces from class names Summary: Attribute values should be extracted without leading or trailing whitespace. Reviewed-by: ohair, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2003-2004 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4882798
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary multi-thread test to exercise sync and contention for removes to transformer registry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Gabriel Adauto, Wily Technology
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run build TransformerManagementThreadRemoveTests
271
696ff142e6e6 4926961: 4/4 TransformerManagementThreadRemoveTests hangs
dcubed
parents: 2
diff changeset
    31
 * @run shell MakeJAR.sh redefineAgent
696ff142e6e6 4926961: 4/4 TransformerManagementThreadRemoveTests hangs
dcubed
parents: 2
diff changeset
    32
 * @run main/othervm -javaagent:redefineAgent.jar TransformerManagementThreadRemoveTests TransformerManagementThreadRemoveTests
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
public class TransformerManagementThreadRemoveTests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    extends TransformerManagementThreadAddTests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     * Constructor for TransformerManagementThreadRemoveTests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * @param name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public TransformerManagementThreadRemoveTests(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    main (String[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        ATestCaseScaffold   test = new TransformerManagementThreadRemoveTests(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        test.runTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    protected final void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    doRunTest()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        testMultiThreadAddsAndRemoves();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    testMultiThreadAddsAndRemoves()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        int size = TOTAL_THREADS + REMOVE_THREADS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        ArrayList threadList = new ArrayList(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        for (int i = MIN_TRANS; i <= MAX_TRANS; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            int index = i - MIN_TRANS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            threadList.add(new TransformerThread("Trans"+prettyNum(index,2), i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        int factor = (int)Math.floor(TOTAL_THREADS / REMOVE_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        for (int i = 0; i < REMOVE_THREADS; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            threadList.add(factor * i, new RemoveThread("Remove"+i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        Thread[] threads = (Thread[])threadList.toArray(new Thread[size]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        setExecThread(new ExecuteTransformersThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        getExecThread().start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        for (int i = threads.length - 1; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            threads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        while (!testCompleted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        {
271
696ff142e6e6 4926961: 4/4 TransformerManagementThreadRemoveTests hangs
dcubed
parents: 2
diff changeset
    89
            // Effective Java - Item 51: Don't depend on the thread scheduler
696ff142e6e6 4926961: 4/4 TransformerManagementThreadRemoveTests hangs
dcubed
parents: 2
diff changeset
    90
            // Use sleep() instead of yield().
696ff142e6e6 4926961: 4/4 TransformerManagementThreadRemoveTests hangs
dcubed
parents: 2
diff changeset
    91
            try {
696ff142e6e6 4926961: 4/4 TransformerManagementThreadRemoveTests hangs
dcubed
parents: 2
diff changeset
    92
                Thread.sleep(500);
696ff142e6e6 4926961: 4/4 TransformerManagementThreadRemoveTests hangs
dcubed
parents: 2
diff changeset
    93
            } catch (InterruptedException ie) {
696ff142e6e6 4926961: 4/4 TransformerManagementThreadRemoveTests hangs
dcubed
parents: 2
diff changeset
    94
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        assertTrue(finalCheck());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        //printTransformers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Method removeTransformer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    removeTransformer(Thread t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        ThreadTransformer tt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        synchronized (fAddedTransformers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            int size = fAddedTransformers.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            if (size > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                int choose = (int)Math.floor(Math.random() * size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                tt = (ThreadTransformer)fAddedTransformers.remove(choose);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            //System.out.println("removed("+tt+") size("+size+") chose("+choose+") by("+t+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (tt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            getInstrumentation().removeTransformer(tt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    RemoveThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        extends Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        RemoveThread(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            while (!threadsDone())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                removeTransformer(RemoveThread.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}