--- /dev/null 2019-07-26 15:01:22.000000000 -0700 +++ new/test/jdk/jdk/jfr/stress/javaagent/JavaAgent.java 2019-07-26 15:01:22.000000000 -0700 @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package jdk.jfr.stress.javaagent; + +import java.lang.instrument.Instrumentation; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.List; +import jdk.jfr.Configuration; +import jdk.jfr.Recording; +import jdk.test.lib.jfr.EventNames; + +// The Java Agent used for testing. +// The test execution happens here, including event generation. +public class JavaAgent { + static final int NUMBER_OF_EVENT_GENERATOR_THREADS = 5; + static final int NUMBER_OF_EVENTS_PER_THREAD = 200*1000; + static final Path RECORDING_FILE_PREMAIN = Paths.get(".", "TestEventsFromJavaAgent-premain.jfr"); + static final Path RECORDING_FILE_AGENTMAIN = Paths.get(".", "TestEventsFromJavaAgent-agentmain.jfr"); + + public static void premain(String agentArgs, Instrumentation inst) throws Exception { + System.out.println("premain(): entering"); + if ("skipPremain".equals(agentArgs)) { + System.out.println("skipping premain()"); + return; + } + + test(RECORDING_FILE_PREMAIN); + System.out.println("premain(): done"); + } + + public static void agentmain(String agentArgs, Instrumentation inst) { + System.out.println("agentmain(): entering"); + + // The try/catch is needed here because, according to the documentation: + // "If the agentmain method throws an uncaught exception it will be ignored by JVM". + try { + test(RECORDING_FILE_AGENTMAIN); + } catch (Exception e) { + System.out.println("agentmain(): exception"); + System.out.println(e.getMessage()); + System.out.println(e.getStackTrace()); + } + + System.out.println("agentmain(): done"); + } + + public static void test(Path recordingFile) throws Exception { + Recording r = new Recording(Configuration.getConfiguration("default")); + r.enable(EventNames.JavaExceptionThrow); + r.setDumpOnExit(true); + r.start(); + + List generators = new ArrayList(); + for (int i=0; i < NUMBER_OF_EVENT_GENERATOR_THREADS; i++) { + generators.add(new EventGenerator(NUMBER_OF_EVENTS_PER_THREAD)); + } + + for (EventGenerator g : generators) { + g.start(); + } + + for (EventGenerator g : generators) { + g.join(); + g.checkErrors(); + } + + r.stop(); + r.dump(recordingFile); + } + + + static class EventGenerator extends Thread { + Exception exception; + int nrOfEventsToGenerate = 1; + + public EventGenerator(int nrOfEventsToGenerate) { + this.nrOfEventsToGenerate = nrOfEventsToGenerate; + } + + void checkErrors() throws Exception { + if (exception != null) { + throw exception; + } + } + + void generateEvent(int count) { + TestEvent ev = new TestEvent(); + ev.msg = "Hello"; + ev.count = count; + ev.thread = this; + ev.clazz = this.getClass(); + ev.commit(); + } + + public void run() { + try { + for (int i=0; i e.getEventType().getName().equals(eventName)) + .count(); + + long expectedEventCount = JavaAgent.NUMBER_OF_EVENT_GENERATOR_THREADS * + JavaAgent.NUMBER_OF_EVENTS_PER_THREAD; + Asserts.assertTrue(testEventCount == expectedEventCount, + "Mismatch in TestEvent count"); + } +}