1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/compiler/profiling/spectrapredefineclass/Agent.java Tue Apr 08 09:51:25 2014 +0200 1.3 @@ -0,0 +1,142 @@ 1.4 +/* 1.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 +import java.security.*; 1.28 +import java.lang.instrument.*; 1.29 +import java.lang.reflect.*; 1.30 +import java.lang.management.ManagementFactory; 1.31 +import com.sun.tools.attach.VirtualMachine; 1.32 + 1.33 +class A { 1.34 + void m() { 1.35 + } 1.36 +} 1.37 + 1.38 +class B extends A { 1.39 + void m() { 1.40 + } 1.41 +} 1.42 + 1.43 +class C extends A { 1.44 + void m() { 1.45 + } 1.46 +} 1.47 + 1.48 +class Test { 1.49 + 1.50 + static public void m() throws Exception { 1.51 + for (int i = 0; i < 20000; i++) { 1.52 + m1(a); 1.53 + } 1.54 + for (int i = 0; i < 4; i++) { 1.55 + m1(b); 1.56 + } 1.57 + } 1.58 + 1.59 + static boolean m1(A a) { 1.60 + boolean res = Agent.m2(a); 1.61 + return res; 1.62 + } 1.63 + 1.64 + static public A a = new A(); 1.65 + static public B b = new B(); 1.66 + static public C c = new C(); 1.67 +} 1.68 + 1.69 +public class Agent implements ClassFileTransformer { 1.70 + 1.71 + 1.72 + static class MemoryChunk { 1.73 + MemoryChunk other; 1.74 + long[] array; 1.75 + MemoryChunk(MemoryChunk other) { 1.76 + other = other; 1.77 + array = new long[1024 * 1024 * 1024]; 1.78 + } 1.79 + } 1.80 + 1.81 + static public boolean m2(A a) { 1.82 + boolean res = false; 1.83 + if (a.getClass() == B.class) { 1.84 + a.m(); 1.85 + } else { 1.86 + res = true; 1.87 + } 1.88 + return res; 1.89 + } 1.90 + 1.91 + static public void main(String[] args) throws Exception { 1.92 + // Create speculative trap entries 1.93 + Test.m(); 1.94 + 1.95 + String nameOfRunningVM = ManagementFactory.getRuntimeMXBean().getName(); 1.96 + int p = nameOfRunningVM.indexOf('@'); 1.97 + String pid = nameOfRunningVM.substring(0, p); 1.98 + 1.99 + // Make the nmethod go away 1.100 + for (int i = 0; i < 10; i++) { 1.101 + System.gc(); 1.102 + } 1.103 + 1.104 + // Redefine class 1.105 + try { 1.106 + VirtualMachine vm = VirtualMachine.attach(pid); 1.107 + vm.loadAgent(System.getProperty("test.classes",".") + "/agent.jar", ""); 1.108 + vm.detach(); 1.109 + } catch (Exception e) { 1.110 + throw new RuntimeException(e); 1.111 + } 1.112 + 1.113 + Test.m(); 1.114 + // GC will hit dead method pointer 1.115 + for (int i = 0; i < 10; i++) { 1.116 + System.gc(); 1.117 + } 1.118 + } 1.119 + 1.120 + public synchronized byte[] transform(final ClassLoader classLoader, 1.121 + final String className, 1.122 + Class<?> classBeingRedefined, 1.123 + ProtectionDomain protectionDomain, 1.124 + byte[] classfileBuffer) { 1.125 + System.out.println("Transforming class " + className); 1.126 + return classfileBuffer; 1.127 + } 1.128 + 1.129 + public static void redefine(String agentArgs, Instrumentation instrumentation, Class to_redefine) { 1.130 + 1.131 + try { 1.132 + instrumentation.retransformClasses(to_redefine); 1.133 + } catch (Exception e) { 1.134 + e.printStackTrace(); 1.135 + } 1.136 + 1.137 + } 1.138 + 1.139 + public static void agentmain(String agentArgs, Instrumentation instrumentation) throws Exception { 1.140 + Agent transformer = new Agent(); 1.141 + instrumentation.addTransformer(transformer, true); 1.142 + 1.143 + redefine(agentArgs, instrumentation, Test.class); 1.144 + } 1.145 +}