Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/CompiledMethodUnload/compmethunload001.java
40948 views
/*1* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.jvmti.CompiledMethodUnload;2425import java.io.*;26import java.math.*;27import java.util.*;2829import nsk.share.*;30import nsk.share.jvmti.*;3132/**33* This test exercises the JVMTI event <code>CompiledMethodUnload</code>.34* <br>It creates an instance of tested class 'HotClass'. Then special35* 'hot' methods are called in a loop in order to be compiled/inlined.36* Then the class is provoked to be unloaded and, thus, CompiledMethodUnload37* events generation for the compiled methods mentioned above.<br>38* The CompiledMethodUnload events, if they occur, must be sent only39* during the live phase of the VM execution.40*/41public class compmethunload001 {4243/**44* class signature to be loaded and then unloaded45*/46private final static String CLS_TO_BE_UNLOADED =47"nsk.jvmti.CompiledMethodUnload.compmethunload001u";4849private final static int MAX_ITERATIONS = 5;5051static {52try {53System.loadLibrary("compmethunload001");54} catch (UnsatisfiedLinkError ule) {55System.err.println("Could not load \"compmethunload001\" library");56System.err.println("java.library.path:"57+ System.getProperty("java.library.path"));58throw ule;59}60}6162native int check();63native int unloaded();6465public static void main(String[] argv) throws Exception {66argv = nsk.share.jvmti.JVMTITest.commonInit(argv);6768// produce JCK-like exit status69System.exit(run(argv, System.out) + Consts.JCK_STATUS_BASE);70}7172public static int run(String argv[], PrintStream out) throws Exception {73return new compmethunload001().runThis(argv, out);74}7576public static void callHotClass(String location) throws Exception {77String clsDir = location + File.separator + "loadclass";7879ClassUnloader clsUnLoader = new ClassUnloader();80// load the class81System.out.println("\nTrying to load class from "82+ clsDir + " ...");83clsUnLoader.loadClass(CLS_TO_BE_UNLOADED, clsDir);8485Class<?> c = clsUnLoader.getLoadedClass();86Object hotCls = c.newInstance();8788// Call hot methods that get compiled89c.getMethod("entryMethod").invoke(hotCls);90c.getMethod("entryNewMethod").invoke(hotCls);91// provoke unloading of previously compiled methods92hotCls = null;93c = null;9495boolean clsUnloaded = clsUnLoader.unloadClass();96clsUnLoader = null;97System.gc();98}99100private int runThis(String argv[], PrintStream out) throws Exception {101ArgumentHandler argHandler = new ArgumentHandler(argv);102String args[] = argHandler.getArguments();103if (args.length < 1)104throw new Failure("TEST BUG: path for class to be loaded is not specified");105106callHotClass(args[0]);107108// Wait until something is unloaded109int num = unloaded();110int iter = 0;111while (num == 0) {112System.gc();113num = unloaded();114iter++;115if (iter > MAX_ITERATIONS) {116throw new Failure("PRODUCT BUG: class was not unloaded in " + MAX_ITERATIONS);117}118}119System.out.println("Number of unloaded events " + num + " number of iterations " + iter);120return check();121}122}123124125