1 /*
   2  * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 package jdk.jfr.javaagent;
  25 
  26 import java.lang.instrument.Instrumentation;
  27 import java.nio.file.Path;
  28 import java.nio.file.Paths;
  29 
  30 import jdk.jfr.Configuration;
  31 import jdk.jfr.Event;
  32 import jdk.jfr.Name;
  33 import jdk.jfr.Recording;
  34 import jdk.jfr.consumer.RecordingFile;
  35 import jdk.test.lib.Asserts;
  36 import jdk.test.lib.jfr.EventNames;
  37 
  38 // Java agent that emits in multiple threads
  39 public class EventEmitterAgent {
  40 
  41     private static final int THREADS = 5;
  42     private static final int EVENTS_PER_THREAD = 150_000;
  43     private static final int EXPECTED_COUNT = THREADS * EVENTS_PER_THREAD;
  44     private static final Path DUMP_PATH = Paths.get("dump.jfr").toAbsolutePath();
  45 
  46     // Called when agent is loaded from command line
  47     public static void agentmain(String agentArgs, Instrumentation inst) throws Exception {
  48         agentWork();
  49     }
  50 
  51     // Called when agent is dynamically loaded
  52     public static void premain(String agentArgs, Instrumentation inst) throws Exception {
  53         agentWork();
  54     }
  55 
  56     private static void agentWork() throws Exception {
  57         try (Recording r = new Recording(Configuration.getConfiguration("default"))) {
  58             r.enable(EventNames.JavaExceptionThrow);
  59             r.setDestination(DUMP_PATH);
  60             r.start();
  61             Thread[] threads = new Thread[THREADS];
  62             for (int i = 0; i < THREADS; i++) {
  63                 threads[i] = new Thread(EventEmitterAgent::emitEvents);
  64                 threads[i].start();
  65             }
  66             for (int i = 0; i < THREADS; i++) {
  67                 threads[i].join();
  68             }
  69             r.stop();
  70         }
  71     }
  72 
  73     public static void emitEvents() {
  74         for (int i = 0; i < EVENTS_PER_THREAD; i++) {
  75             TestEvent e = new TestEvent();
  76             e.msg = "Long message that puts pressure on the string pool " + i % 100;
  77             e.count = i;
  78             e.thread = Thread.currentThread();
  79             e.clazz = String.class;
  80             e.commit();
  81             if (i % 10000 == 0) {
  82                 try {
  83                     Thread.sleep(1);
  84                 } catch (InterruptedException ie) {
  85                     // ignore
  86                 }
  87             }
  88         }
  89     }
  90 
  91     @Name("Test")
  92     static class TestEvent extends Event {
  93         String msg;
  94         int count;
  95         Thread thread;
  96         Class<?> clazz;
  97     }
  98 
  99     public static void validateRecording() throws Exception {
 100         long testEventCount = RecordingFile.readAllEvents(DUMP_PATH)
 101                 .stream()
 102                 .filter(e -> e.getEventType().getName().equals("Test"))
 103                 .count();
 104         Asserts.assertTrue(testEventCount == EXPECTED_COUNT, "Mismatch in TestEvent count");
 105     }
 106 }