jdk/test/java/lang/instrument/TransformerManagementThreadRemoveTests.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 271 696ff142e6e6
permissions -rw-r--r--
Initial load
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
 * @ignore Disabled until race condition which hangs test can be fixed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build TransformerManagementThreadRemoveTests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run shell MakeJAR.sh basicAgent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main/othervm -javaagent:basicAgent.jar TransformerManagementThreadRemoveTests TransformerManagementThreadRemoveTests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class TransformerManagementThreadRemoveTests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    extends TransformerManagementThreadAddTests
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
     * Constructor for TransformerManagementThreadRemoveTests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * @param name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public TransformerManagementThreadRemoveTests(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    main (String[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        ATestCaseScaffold   test = new TransformerManagementThreadRemoveTests(args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        test.runTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected final void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    doRunTest()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        testMultiThreadAddsAndRemoves();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    testMultiThreadAddsAndRemoves()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        int size = TOTAL_THREADS + REMOVE_THREADS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        ArrayList threadList = new ArrayList(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        for (int i = MIN_TRANS; i <= MAX_TRANS; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            int index = i - MIN_TRANS;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            threadList.add(new TransformerThread("Trans"+prettyNum(index,2), i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        int factor = (int)Math.floor(TOTAL_THREADS / REMOVE_THREADS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        for (int i = 0; i < REMOVE_THREADS; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            threadList.add(factor * i, new RemoveThread("Remove"+i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        Thread[] threads = (Thread[])threadList.toArray(new Thread[size]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        setExecThread(new ExecuteTransformersThread());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        getExecThread().start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        for (int i = threads.length - 1; i >= 0; i--)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            threads[i].start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        while (!testCompleted())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            Thread.currentThread().yield();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        assertTrue(finalCheck());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        //printTransformers();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * Method removeTransformer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    private void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    removeTransformer(Thread t)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        ThreadTransformer tt = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        synchronized (fAddedTransformers)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            int size = fAddedTransformers.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            if (size > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                int choose = (int)Math.floor(Math.random() * size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                tt = (ThreadTransformer)fAddedTransformers.remove(choose);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            //System.out.println("removed("+tt+") size("+size+") chose("+choose+") by("+t+")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        if (tt != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            getInstrumentation().removeTransformer(tt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private class
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    RemoveThread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        extends Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        RemoveThread(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        public void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            while (!threadsDone())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                removeTransformer(RemoveThread.this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}