test/jdk/jdk/jfr/api/consumer/streaming/TestProcess.java
changeset 59226 a0f39cc47387
child 59246 fcad92f425c5
equal deleted inserted replaced
59225:80e1201f6c9a 59226:a0f39cc47387
       
     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.  Oracle designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Oracle in the LICENSE file that accompanied this code.
       
    10  *
       
    11  * This code is distributed in the hope that it will be useful, but WITHOUT
       
    12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
       
    13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
       
    14  * version 2 for more details (a copy is included in the LICENSE file that
       
    15  * accompanied this code).
       
    16  *
       
    17  * You should have received a copy of the GNU General Public License version
       
    18  * 2 along with this work; if not, write to the Free Software Foundation,
       
    19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
       
    20  *
       
    21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    22  * or visit www.oracle.com if you need additional information or have any
       
    23  * questions.
       
    24  */
       
    25 package jdk.jfr.api.consumer.streaming;
       
    26 
       
    27 import java.io.IOException;
       
    28 import java.io.RandomAccessFile;
       
    29 import java.nio.file.Files;
       
    30 import java.nio.file.Path;
       
    31 import java.nio.file.Paths;
       
    32 import java.util.Properties;
       
    33 
       
    34 import jdk.internal.misc.Unsafe;
       
    35 import jdk.jfr.Event;
       
    36 import jdk.test.lib.process.ProcessTools;
       
    37 
       
    38 import com.sun.tools.attach.VirtualMachine;
       
    39 
       
    40 /**
       
    41  * Class that emits a NUMBER_OF_EVENTS and then awaits crash or exit
       
    42  *
       
    43  * Requires jdk.attach module.
       
    44  *
       
    45  */
       
    46 public final class TestProcess {
       
    47 
       
    48     private static class TestEvent extends Event {
       
    49     }
       
    50 
       
    51     public final static int NUMBER_OF_EVENTS = 10;
       
    52 
       
    53     private final Process process;
       
    54     private final Path path;
       
    55 
       
    56     public TestProcess(String name) throws IOException {
       
    57         this.path = Paths.get("action-" + System.currentTimeMillis()).toAbsolutePath();
       
    58         String[] args = {
       
    59                 "--add-exports",
       
    60                 "java.base/jdk.internal.misc=ALL-UNNAMED",
       
    61                 "-XX:StartFlightRecording:settings=none",
       
    62                 TestProcess.class.getName(), path.toString()
       
    63             };
       
    64         ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(false, args);
       
    65         process = ProcessTools.startProcess(name, pb);
       
    66     }
       
    67 
       
    68     public static void main(String... args) throws Exception {
       
    69         for (int i = 0; i < NUMBER_OF_EVENTS; i++) {
       
    70             TestEvent e = new TestEvent();
       
    71             e.commit();
       
    72         }
       
    73 
       
    74         Path path = Paths.get(args[0]);
       
    75         while (true) {
       
    76             try {
       
    77                 String action = Files.readString(path);
       
    78                 if ("crash".equals(action)) {
       
    79                     System.out.println("About to crash...");
       
    80                     Unsafe.getUnsafe().putInt(0L, 0);
       
    81                 }
       
    82                 if ("exit".equals(action)) {
       
    83                     System.out.println("About to exit...");
       
    84                     System.exit(0);
       
    85                 }
       
    86             } catch (Exception ioe) {
       
    87                 // Ignore
       
    88             }
       
    89             takeNap();
       
    90         }
       
    91     }
       
    92 
       
    93     public Path getRepository() {
       
    94         while (true) {
       
    95             try {
       
    96                 VirtualMachine vm = VirtualMachine.attach(String.valueOf(process.pid()));
       
    97                 Properties p = vm.getSystemProperties();
       
    98                 vm.detach();
       
    99                 String repo = (String) p.get("jdk.jfr.repository");
       
   100                 if (repo != null) {
       
   101                     return Paths.get(repo);
       
   102                 }
       
   103             } catch (Exception e) {
       
   104                 System.out.println("Attach failed: " + e.getMessage());
       
   105                 System.out.println("Retrying...");
       
   106             }
       
   107             takeNap();
       
   108         }
       
   109     }
       
   110 
       
   111     private static void takeNap() {
       
   112         try {
       
   113             Thread.sleep(10);
       
   114         } catch (InterruptedException ie) {
       
   115             // ignore
       
   116         }
       
   117     }
       
   118 
       
   119     public void crash() {
       
   120         try {
       
   121             Files.writeString(path, "crash");
       
   122         } catch (IOException ioe) {
       
   123             ioe.printStackTrace();
       
   124         }
       
   125     }
       
   126 
       
   127     public void exit() {
       
   128         try {
       
   129             Files.writeString(path, "exit");
       
   130         } catch (IOException ioe) {
       
   131             ioe.printStackTrace();
       
   132         }
       
   133     }
       
   134 
       
   135     public long pid() {
       
   136         return process.pid();
       
   137     }
       
   138 }