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 import java.lang.management.ManagementFactory;
  33 
  34 import jdk.jfr.Configuration;
  35 import jdk.jfr.Recording;
  36 import jdk.test.lib.jfr.FileHelper;
  37 import jdk.test.lib.process.OutputAnalyzer;
  38 
  39 /**
  40  * @test
  41  * @summary The test verifies JFR.dump command
  42  * @key jfr
  43  *
  44  * @library /lib /
  45  * @run main/othervm jdk.jfr.jcmd.TestJcmdDumpGeneratedFilename
  46  */
  47 public class TestJcmdDumpGeneratedFilename {
  48 
  49     public static void main(String[] args) throws Exception {
  50         // Increase the id for a recording
  51         for (int i = 0; i < 300; i++) {
  52             new Recording();
  53         }
  54         try (Recording r = new Recording(Configuration.getConfiguration("default"))) {
  55             r.start();
  56             r.stop();
  57             testDumpFilename();
  58             testDumpFilename(r);
  59             testDumpDiectory();
  60             testDumpDiectory(r);
  61         }
  62     }
  63 
  64     private static void testDumpFilename() throws Exception {
  65         OutputAnalyzer output = JcmdHelper.jcmd("JFR.dump");
  66         verifyFile(readFilename(output), null);
  67     }
  68 
  69     private static void testDumpFilename(Recording r) throws Exception {
  70         OutputAnalyzer output = JcmdHelper.jcmd("JFR.dump", "name=" + r.getId());
  71         verifyFile(readFilename(output), r.getId());
  72     }
  73 
  74     private static void testDumpDiectory() throws Exception {
  75         Path directory = Paths.get(".").toAbsolutePath().normalize();
  76         OutputAnalyzer output = JcmdHelper.jcmd("JFR.dump", "filename=" + directory);
  77         String filename = readFilename(output);
  78         verifyFile(filename, null);
  79         verifyDirectory(filename, directory);
  80     }
  81 
  82     private static void testDumpDiectory(Recording r) throws Exception {
  83         Path directory = Paths.get(".").toAbsolutePath().normalize();
  84         OutputAnalyzer output = JcmdHelper.jcmd("JFR.dump", "name=" + r.getId(), "filename=" + directory);
  85         String filename = readFilename(output);
  86         verifyFile(filename, r.getId());
  87         verifyDirectory(filename, directory);
  88     }
  89 
  90     private static void verifyDirectory(String filename, Path directory) throws Exception {
  91         if (!filename.contains(directory.toAbsolutePath().normalize().toString())) {
  92             throw new Exception("Expected dump to be at " + directory);
  93         }
  94     }
  95 
  96     private static long getProcessId() {
  97 
  98         // something like '<pid>@<hostname>', at least in SUN / Oracle JVMs
  99         final String jvmName = ManagementFactory.getRuntimeMXBean().getName();
 100 
 101         final int index = jvmName.indexOf('@');
 102 
 103         if (index < 1) {
 104             // part before '@' empty (index = 0) / '@' not found (index = -1)
 105             return 42;
 106         }
 107 
 108         try {
 109             return Long.parseLong(jvmName.substring(0, index));
 110         } catch (NumberFormatException e) {
 111             // ignore
 112         }
 113         return 42;
 114     }
 115 
 116     private static void verifyFile(String filename, Long id) throws Exception {
 117         String idText = id == null ? "" : "-id-" + Long.toString(id);
 118         String expectedName = "hotspot-pid-" + getProcessId() + idText;
 119         if (!filename.contains(expectedName)) {
 120             throw new Exception("Expected filename to contain " + expectedName);
 121         }
 122         FileHelper.verifyRecording(new File(filename));
 123     }
 124 
 125     private static String readFilename(OutputAnalyzer output) throws Exception {
 126         Iterator<String> it = output.asLines().iterator();
 127         while (it.hasNext()) {
 128             String line = it.next();
 129             if (line.contains("written to")) {
 130                 line = it.next(); // blank line
 131                 return it.next();
 132             }
 133         }
 134         throw new Exception("Could not find filename of dumped recording.");
 135     }
 136 }