1 /* 2 * Copyright (c) 2015, 2018, 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.jfr.jcmd; 27 28 import java.io.File; 29 import java.nio.file.Path; 30 import java.nio.file.Paths; 31 import java.util.Iterator; 32 33 import jdk.jfr.Configuration; 34 import jdk.jfr.Recording; 35 import jdk.test.lib.jfr.FileHelper; 36 import jdk.test.lib.process.OutputAnalyzer; 37 38 /** 39 * @test 40 * @summary The test verifies JFR.dump command 41 * @key jfr 42 * @requires vm.hasJFR 43 * @library /test/lib /test/jdk 44 * @run main/othervm jdk.jfr.jcmd.TestJcmdDumpGeneratedFilename 45 */ 46 public class TestJcmdDumpGeneratedFilename { 47 48 public static void main(String[] args) throws Exception { 49 // Increase the id for a recording 50 for (int i = 0; i < 300; i++) { 51 new Recording(); 52 } 53 try (Recording r = new Recording(Configuration.getConfiguration("default"))) { 54 r.start(); 55 r.stop(); 56 testDumpFilename(); 57 testDumpFilename(r); 58 testDumpDiectory(); 59 testDumpDiectory(r); 60 } 61 } 62 63 private static void testDumpFilename() throws Exception { 64 OutputAnalyzer output = JcmdHelper.jcmd("JFR.dump"); 65 verifyFile(readFilename(output), null); 66 } 67 68 private static void testDumpFilename(Recording r) throws Exception { 69 OutputAnalyzer output = JcmdHelper.jcmd("JFR.dump", "name=" + r.getId()); 70 verifyFile(readFilename(output), r.getId()); 71 } 72 73 private static void testDumpDiectory() throws Exception { 74 Path directory = Paths.get(".").toAbsolutePath().normalize(); 75 OutputAnalyzer output = JcmdHelper.jcmd("JFR.dump", "filename=" + directory); 76 String filename = readFilename(output); 77 verifyFile(filename, null); 78 verifyDirectory(filename, directory); 79 } 80 81 private static void testDumpDiectory(Recording r) throws Exception { 82 Path directory = Paths.get(".").toAbsolutePath().normalize(); 83 OutputAnalyzer output = JcmdHelper.jcmd("JFR.dump", "name=" + r.getId(), "filename=" + directory); 84 String filename = readFilename(output); 85 verifyFile(filename, r.getId()); 86 verifyDirectory(filename, directory); 87 } 88 89 private static void verifyDirectory(String filename, Path directory) throws Exception { 90 if (!filename.contains(directory.toAbsolutePath().normalize().toString())) { 91 throw new Exception("Expected dump to be at " + directory); 92 } 93 } 94 95 private static void verifyFile(String filename, Long id) throws Exception { 96 String idText = id == null ? "" : "-id-" + Long.toString(id); 97 String expectedName = "hotspot-pid-" + ProcessHandle.current().pid() + idText; 98 if (!filename.contains(expectedName)) { 99 throw new Exception("Expected filename to contain " + expectedName); 100 } 101 FileHelper.verifyRecording(new File(filename)); 102 } 103 104 private static String readFilename(OutputAnalyzer output) throws Exception { 105 Iterator<String> it = output.asLines().iterator(); 106 while (it.hasNext()) { 107 String line = it.next(); 108 if (line.contains("written to")) { 109 line = it.next(); // blank line 110 return it.next(); 111 } 112 } 113 throw new Exception("Could not find filename of dumped recording."); 114 } 115 }