1 /* 2 * Copyright (c) 2016, 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 26 package jdk.testlibrary.jfr; 27 28 import java.util.ArrayList; 29 import java.util.Collections; 30 import java.util.List; 31 32 import jdk.testlibrary.OutputAnalyzer; 33 import jdk.testlibrary.ProcessTools; 34 35 36 /** 37 * Helper class for running applications with enabled JFR recording 38 */ 39 public class AppExecutorHelper { 40 41 /** 42 * Executes an application with enabled JFR and writes collected events 43 * to the given output file. 44 * Which events to track and other parameters are taken from the setting .jfc file. 45 * 46 * @param setting JFR settings file(optional) 47 * @param jfrFilename JFR resulting recording filename(optional) 48 * @param additionalVMFlags additional VM flags passed to the java(optional) 49 * @param className name of the class to execute 50 * @param classArguments arguments passed to the class(optional) 51 * @return output analyzer for executed application 52 */ 53 public static OutputAnalyzer executeAndRecord(String settings, String jfrFilename, String[] additionalVmFlags, 54 String className, String... classArguments) throws Exception { 55 List<String> arguments = new ArrayList<>(); 56 String baseStartFlightRecording = "-XX:StartFlightRecording"; 57 String additionalStartFlightRecording = ""; 58 59 if (additionalVmFlags != null) { 60 Collections.addAll(arguments, additionalVmFlags); 61 } 62 63 if (settings != null & jfrFilename != null) { 64 additionalStartFlightRecording = String.format("=settings=%s,filename=%s", settings, jfrFilename); 65 } else if (settings != null) { 66 additionalStartFlightRecording = String.format("=settings=%s", settings); 67 } else if (jfrFilename != null) { 68 additionalStartFlightRecording = String.format("=filename=%s", jfrFilename); 69 } 70 arguments.add(baseStartFlightRecording + additionalStartFlightRecording); 71 72 arguments.add(className); 73 if (classArguments.length > 0) { 74 Collections.addAll(arguments, classArguments); 75 } 76 77 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, arguments.toArray(new String[0])); 78 OutputAnalyzer output = null; 79 try { 80 output = ProcessTools.executeProcess(pb); 81 } catch (Throwable e) { 82 e.printStackTrace(); 83 } 84 return output; 85 } 86 }