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.nio.file.Files; 29 import java.nio.file.Path; 30 import java.util.Arrays; 31 import java.util.regex.Matcher; 32 import java.util.regex.Pattern; 33 34 import jdk.jfr.Configuration; 35 import jdk.jfr.Event; 36 import jdk.jfr.Recording; 37 import jdk.testlibrary.OutputAnalyzer; 38 import jdk.testlibrary.ProcessTools;; 39 40 public class ExecuteHelper { 41 42 public static Object[] array; 43 44 static class CustomEvent extends Event { 45 int intValue; 46 long longValue; 47 double doubliValue; 48 float floatValue; 49 String stringValue; 50 Short shortValue; 51 boolean booleanValue; 52 char charValue; 53 double trickyDouble; 54 } 55 56 public static OutputAnalyzer run(String... args) { 57 String[] array = new String[args.length + 1]; 58 System.arraycopy(args, 0, array, 1, args.length); 59 array[0] = "jdk.jfr.internal.cmd.Execute"; 60 OutputAnalyzer output = null; 61 try { 62 output = ProcessTools.executeTestJvm(array); 63 } catch (Throwable e) { 64 e.printStackTrace(); 65 //String message = String.format("Caught exception while executing '%s'", Arrays.asList(array)); 66 //throw new RuntimeException(message, e); 67 } 68 return output; 69 } 70 71 public static void emitCustomEvents() { 72 // Custom events with potentially tricky values 73 CustomEvent event1 = new CustomEvent(); 74 event1.trickyDouble = Double.NaN; 75 event1.intValue = Integer.MIN_VALUE; 76 event1.longValue = Long.MIN_VALUE; 77 event1.doubliValue = Double.MIN_VALUE; 78 event1.floatValue = Float.MIN_VALUE; 79 StringBuilder sb = new StringBuilder(); 80 for (int i = 0; i < 512; i++) { 81 sb.append((char) i); 82 } 83 sb.append("\u2324"); 84 event1.stringValue = sb.toString(); 85 event1.shortValue = Short.MIN_VALUE; 86 event1.booleanValue = true; 87 event1.booleanValue = false; 88 event1.charValue = '\b'; 89 event1.commit(); 90 91 CustomEvent event2 = new CustomEvent(); 92 event2.trickyDouble = Double.NEGATIVE_INFINITY; 93 event2.intValue = Integer.MAX_VALUE; 94 event2.longValue = Long.MAX_VALUE; 95 event2.doubliValue = Double.MAX_VALUE; 96 event2.floatValue = Float.MAX_VALUE; 97 event2.stringValue = null; 98 event2.shortValue = Short.MAX_VALUE; 99 event2.booleanValue = false; 100 event2.charValue = 0; 101 event2.commit(); 102 } 103 104 public static Path createProfilingRecording() throws Exception { 105 Path file = Files.createTempFile("recording", ".jfr"); 106 // Create a recording with some data 107 try (Recording r = new Recording(Configuration.getConfiguration("profile"))) { 108 r.start(); 109 110 // Allocation event 111 array = new Object[1000000]; 112 array = null; 113 114 // Class loading event etc 115 provokeClassLoading(); 116 117 // GC events 118 System.gc(); 119 120 // ExecutionSample 121 long t = System.currentTimeMillis(); 122 while (System.currentTimeMillis() - t < 50) { 123 // do nothing 124 } 125 126 // Other periodic events, i.e CPU load 127 Thread.sleep(1000); 128 129 r.stop(); 130 r.dump(file); 131 } 132 133 return file; 134 } 135 136 private static void provokeClassLoading() { 137 // Matching a string with regexp 138 // is expected to load some classes and generate some VM events 139 Pattern p = Pattern.compile("a*b"); 140 Matcher m = p.matcher("aaaaab"); 141 m.matches(); 142 } 143 }