Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/AttachOnDemand/attach045/attach045Target.java
40951 views
/*1* Copyright (c) 2007, 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*/22package nsk.jvmti.AttachOnDemand.attach045;2324import nsk.share.CustomClassLoader;25import nsk.share.TestBug;26import nsk.share.aod.*;27import java.util.*;2829/*30* This target application provokes following events:31* - ClassLoad32* - ClassPrepare33* - ThreadStart34* - ThreadEnd35* - VMObjectAlloc36*37* Note: number of generated events should be up-to-date with numbers expected by test agents38*/39public class attach045Target extends TargetApplicationWaitingAgents {4041static final String classPathOption = "classPath";4243List<Thread> threadsGeneratingEvents = new ArrayList<Thread>();4445static class ArgParser extends AODTargetArgParser {46ArgParser(String[] args) {47super(args);48}4950protected boolean checkOption(String option, String value) {51if (super.checkOption(option, value))52return true;5354if (option.equals(classPathOption))55return true;5657return false;58}5960protected void checkOptions() {61super.checkOptions();6263if (!options.containsKey(classPathOption))64throw new TestBug("Classpath wasn't specified");65}6667String getClasspath() {68return options.getProperty(classPathOption);69}70}7172protected AODTargetArgParser createArgParser(String[] args) {73return new ArgParser(args);74}7576protected void init(String[] args) {77for (int i = 0; i < 10; i++) {78ClassEventsThread thread = new ClassEventsThread(50, ((ArgParser)argParser).getClasspath());79thread.setName("attach045-ClassEventsThread-" + i);80threadsGeneratingEvents.add(thread);81}82}8384static final String CLASS_TO_LOAD_NAME = "nsk.jvmti.AttachOnDemand.attach045.ClassToLoad";8586static class TestClassLoader extends CustomClassLoader {87public Class<?> loadClass(String name) throws ClassNotFoundException {88if (name.equals(CLASS_TO_LOAD_NAME))89return findClass(name);90else91return super.loadClass(name);92}93}9495class ClassEventsThread extends Thread {96int eventsNumber;97String classPath;9899ClassEventsThread(int eventsNumber, String classPath) {100this.eventsNumber = eventsNumber;101this.classPath = classPath;102}103104public void run() {105try {106for (int i = 0; i < eventsNumber; i++) {107/*108* Provoke ClassLoad/ClassPrepare events109*/110TestClassLoader classLoader = new TestClassLoader();111classLoader.setClassPath(classPath);112Class<?> klass = Class.forName(CLASS_TO_LOAD_NAME, true, classLoader);113114/*115* Provoke VMObjectAlloc event116*/117klass.newInstance();118}119} catch (Throwable t) {120setStatusFailed(t);121}122}123}124125protected void targetApplicationActions() throws Throwable {126for (Thread thread : threadsGeneratingEvents) {127thread.start();128}129130log.display("Starting to generate events");131132/*133* Provoke ThreadStart/ThreadEnd events134*/135for (int i = 0; i < 100; i++) {136Thread thread = new Thread(new Runnable() {137public void run() {138// do nothing139}140});141thread.setName("attach045-ThreadStartEndEventsThread-" + i);142thread.start();143thread.join();144}145146for (Thread thread : threadsGeneratingEvents) {147thread.join();148}149150log.display("All events were generated");151}152153public static void main(String[] args) {154new attach045Target().runTargetApplication(args);155}156}157158159