hotspot/test/compiler/profiling/spectrapredefineclass/Agent.java
author vdeshpande
Fri, 26 Aug 2016 12:20:09 -0700
changeset 41362 e09e871860a7
parent 40059 c2304140ed64
child 40651 998e35976abf
permissions -rw-r--r--
8154122: Intrinsify fused mac operations Summary: added FMA intrinsics on x86 Reviewed-by: kvn, aph, darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23845
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
     1
/*
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
     4
 *
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
     8
 *
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    13
 * accompanied this code).
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    14
 *
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    18
 *
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    21
 * questions.
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    22
 */
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    23
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 24310
diff changeset
    24
package compiler.profiling.spectrapredefineclass;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 24310
diff changeset
    25
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 24310
diff changeset
    26
import com.sun.tools.attach.VirtualMachine;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 24310
diff changeset
    27
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 24310
diff changeset
    28
import java.lang.instrument.ClassFileTransformer;
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 24310
diff changeset
    29
import java.lang.instrument.Instrumentation;
23845
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    30
import java.lang.management.ManagementFactory;
40059
c2304140ed64 8132919: Put compiler tests in packages
tpivovarova
parents: 24310
diff changeset
    31
import java.security.ProtectionDomain;
23845
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    32
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    33
class A {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    34
    void m() {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    35
    }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    36
}
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    37
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    38
class B extends A {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    39
    void m() {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    40
    }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    41
}
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    42
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    43
class C extends A {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    44
    void m() {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    45
    }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    46
}
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    47
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    48
class Test {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    49
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    50
    static public void m() throws Exception {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    51
        for (int i = 0; i < 20000; i++) {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    52
            m1(a);
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    53
        }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    54
        for (int i = 0; i < 4; i++) {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    55
            m1(b);
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    56
        }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    57
    }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    58
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    59
    static boolean m1(A a) {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    60
        boolean res =  Agent.m2(a);
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    61
        return res;
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    62
    }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    63
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    64
    static public A a = new A();
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    65
    static public B b = new B();
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    66
    static public C c = new C();
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    67
}
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    68
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    69
public class Agent implements ClassFileTransformer {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    70
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    71
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    72
    static public boolean m2(A a) {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    73
        boolean res = false;
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    74
        if (a.getClass() == B.class) {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    75
            a.m();
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    76
        } else {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    77
            res = true;
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    78
        }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    79
        return res;
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    80
    }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    81
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    82
    static public void main(String[] args) throws Exception {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    83
        // Create speculative trap entries
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    84
        Test.m();
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    85
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    86
        String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName();
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    87
        int p = nameOfRunningVM.indexOf('@');
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    88
        String pid = nameOfRunningVM.substring(0, p);
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    89
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    90
        // Make the nmethod go away
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    91
        for (int i = 0; i < 10; i++) {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    92
            System.gc();
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    93
        }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    94
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    95
        // Redefine class
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    96
        try {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    97
            VirtualMachine vm = VirtualMachine.attach(pid);
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    98
            vm.loadAgent(System.getProperty("test.classes",".") + "/agent.jar", "");
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
    99
            vm.detach();
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   100
        } catch (Exception e) {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   101
            throw new RuntimeException(e);
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   102
        }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   103
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   104
        Test.m();
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   105
        // GC will hit dead method pointer
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   106
        for (int i = 0; i < 10; i++) {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   107
            System.gc();
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   108
        }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   109
    }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   110
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   111
    public synchronized byte[] transform(final ClassLoader classLoader,
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   112
                                         final String className,
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   113
                                         Class<?> classBeingRedefined,
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   114
                                         ProtectionDomain protectionDomain,
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   115
                                         byte[] classfileBuffer) {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   116
        System.out.println("Transforming class " + className);
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   117
        return classfileBuffer;
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   118
    }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   119
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   120
    public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   121
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   122
        try {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   123
            instrumentation.retransformClasses(to_redefine);
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   124
        } catch (Exception e) {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   125
            e.printStackTrace();
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   126
        }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   127
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   128
    }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   129
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   130
    public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception {
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   131
        Agent transformer = new Agent();
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   132
        instrumentation.addTransformer(transformer, true);
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   133
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   134
        redefine(agentArgs, instrumentation, Test.class);
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   135
    }
ebd1fafcc362 8038636: speculative traps break when classes are redefined
roland
parents:
diff changeset
   136
}